Skip to main content

Overview

We’ll create a FastAPI application that:
  1. Accepts support ticket submissions
  2. Uses an LLM to classify the ticket
  3. Returns the classification result
  4. Traces the entire process with Laminar

Setup

You can also use the example app from our GitHub repo. Alternatively, for a clean install, follow the steps below:
1

Install Dependencies

First, let’s install the required packages:
2

Environment Setup

Create a .env file with your API keys:
3

Project Structure

Create the following project structure:

Implementation

Let’s create our FastAPI application with Laminar tracing. We’ll split the code into three files:

1. schemas.py

This file will contain the Pydantic models for the request and response.
schemas.py

2. llm.py

This file will contain the logic that handles the LLM. In a production app, this will likely be a much bigger module with classes, routing, etc. For now, we’ll just have a function that handles an OpenAI call.
llm.py

3. main.py

This file will contain the logic that handles the FastAPI app.
Laminar must be initialized once at the entry point of the application. For FastAPI, this is typically main.py, right before creating app.
main.py

Running the Application

Start the FastAPI server:

Testing the API

You can test the API using curl:

Viewing Traces

After making a request, you can view the traces in your Laminar dashboard at https://www.lmnr.ai. The trace will show:
  1. The ticket classification function execution
  2. The OpenAI API call

Key Features Demonstrated

  1. Automatic Tracing: Laminar automatically traces OpenAI calls and functions marked with @observe
  2. Tokens Usage: Laminar automatically calculates the tokens used for each OpenAI call
  3. Cost Estimation: Laminar automatically estimates the cost of each OpenAI call

Troubleshooting

If you encounter issues:
  • Check that your API keys are correctly set in the .env file and that it is loaded correctly
  • Verify that Laminar is properly initialized
  • Ensure all dependencies are installed
  • Review the FastAPI logs for any application errors