FastAPI Instrumentation Example
FastAPI Integration with Laminar Tracing
Overview
We’ll create a FastAPI application that:
- Accepts support ticket submissions
- Uses an LLM to classify the ticket
- Returns the classification result
- 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:
Install Dependencies
First, let’s install the required packages:
Environment Setup
Create a .env
file with your API keys:
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.
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.
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
.
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:
- The ticket classification function execution
- The OpenAI API call
Key Features Demonstrated
- Automatic Tracing: Laminar automatically traces OpenAI calls and functions marked with
@observe
- Tokens Usage: Laminar automatically calculates the tokens used for each OpenAI call
- 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