Prerequisites
Before you begin, make sure you have:- A LangSmith account: Sign up or log in at smith.langchain.com.
- A LangSmith API key: Go to Settings > API Keys and create a new API key.
- An OpenAI API key: Generate this from the OpenAI dashboard.
1. Create a directory and install dependencies
In your terminal, create a directory for your project and install the dependencies in your environment:2. Set up environment variables
Set the following environment variables:LANGSMITH_TRACINGLANGSMITH_API_KEYOPENAI_API_KEY(or your LLM provider’s API key)- (optional)
LANGSMITH_WORKSPACE_ID: If your LangSmith API key is linked to multiple workspaces, set this variable to specify which workspace to use.
To send traces to a specific project, use the
LANGSMITH_PROJECT environment variable. If this is not set, LangSmith will create a default tracing project automatically on trace ingestion.3. Build your application
In this step, you’ll build a simple agent that uses OpenAI’s Responses API with function calling. This example shows a weather assistant that can look up weather information. For simplicity, you’ll return a static forecast.4. Add Tracing
Now that we’ve defined our application, let’s add tracing to it with just three changes:- Import LangSmith utilities -
wrap_openaiandtraceable. - Wrap the OpenAI client - Automatically traces all OpenAI calls.
- Wrap tool functions - Creates runs for tool execution, can also be used to trace other functions.
5. Run the code
Execute the script:6. View the Trace in LangSmith
In the LangSmith UI, navigate to the default Tracing Project for the workspace you specified in Step 2). You’ll see the agent’s trace. It should look like this: https://smith.langchain.com/public/c1ac97f8-a022-4e0f-8380-59cab47f28e5/r.Next steps
Here are some topics you might want to explore next:- Tracing integrations provide support for various LLM providers and agent frameworks.
- Filtering traces can help you effectively navigate and analyze data in tracing projects that contain a significant amount of data.
- Trace a RAG application is a full tutorial, which adds observability to an application from development through to production.
- Sending traces to a specific project changes the destination project of your traces.
After logging traces, use Polly to analyze them and get AI-powered insights into your application’s performance.