Skip to main content
LangSmith gives you end-to-end visibility into your agent by capturing traces; a complete record of every step that ran during a request, from the inputs passed in to the final output returned. In this quickstart, you will add tracing to an AI assistant and view the results in LangSmith. Traces belong to an agent and one of its environments within a workspace. This quickstart creates the agent in LangSmith, then configures your code to send traces to its identifier and environment. Compatibility instructions cover older workspaces and self-hosted LangSmith.
If you’re building with LangChain or LangGraph, you can enable LangSmith tracing with a single environment variable. Refer to trace with LangChain or trace with LangGraph.

Prerequisites

Before you begin, make sure you have: Select your provider in the tabs on each step. The Anthropic and Gemini examples are in Python and TypeScript only, because the LangSmith Java SDK wraps only the OpenAI client.

1. Set up your environment

  1. Create a directory for your agent code, install the dependencies, and configure the required environment variables:
  2. Export your environment variables in your shell:
    Before sending traces, create the agent in LangSmith:
    1. In an agent-based workspace, open + Agent, choose Observability, and create an agent with an Agent name and Agent ID. This requires the projects:create permission. If the agent already exists, reuse it.
    2. Copy the Agent ID and choose an environment: local, development, staging, or production.
    3. Configure agent addressing before running the agent:
    Register the agent once, not for every trace. Remove LANGSMITH_PROJECT, LANGCHAIN_PROJECT, and LANGCHAIN_SESSION from the environment where your code runs. Do not combine legacy destination settings with agent addressing. To create the agent with an SDK call or API request, see Create an agent programmatically.
    Agent addressing is available in agent-based workspaces on LangSmith Cloud. In a project-based workspace, including self-hosted LangSmith, set LANGSMITH_PROJECT to your tracing project name instead of setting the two agent variables.
    If your account is in a region other than US (the default), also set LANGSMITH_ENDPOINT to the API URL for your region. Without this, your API key won’t be recognized and requests will fail to authenticate.
    Region
    GCP US
    GCP EU
    GCP APAC
    AWS US
    For example, EU accounts: export LANGSMITH_ENDPOINT="https://eu.api.smith.langchain.com".
    For other providers, use the @traceable decorator to trace calls manually.

2. Build the app

The following app uses two LangSmith tools to add tracing:
  • Provider wrapper: wraps the OpenAI, Anthropic, or Gemini client so every LLM call is automatically logged as a nested span. The Gemini wrapper is in beta.
  • Traceable wrapper: wraps a function so its inputs, outputs, and any nested spans appear as a single trace in LangSmith. Use @traceable in Python, traceable in TypeScript and Kotlin, and Tracing.traceFunction in Java.
The assistant function calls a tool (get_context) to retrieve relevant context, then passes that context to the model. Using the traceable wrapper on both functions captures the full pipeline in one trace, with the tool call and LLM call as nested spans. Create a file called app.py, index.ts, App.java, or App.kt with the following code:

3. Run the app

4. View your trace

In the LangSmith UI, select your agent, go to Tracing, and select the environment configured during setup. In a project-based workspace, open the project named by LANGSMITH_PROJECT. For more information, see Navigate agents. Click the assistant row to open the trace. The Trajectory tab shows the conversation as it was sent to the model. Select the Details tab to see the full run tree, including the assistant function with the get_context tool call and the model call nested inside it. LangSmith UI showing a trace with an outer agent span and a nested LLM call span. LangSmith UI showing a trace with an outer agent span and a nested LLM call span. The outer span captures your assistant function’s inputs and outputs. The nested get_context span (getContext in TypeScript) records the tool call, and the model span (ChatOpenAI, ChatAnthropic, or ChatGoogleGenerativeAI) records the exact prompt sent to the model and the response returned.
You can also inspect traces from the terminal using the LangSmith CLI.

Next steps

After logging traces, use Chat to analyze them and get AI-powered insights into your agent’s performance.