Skip to main content
Mastra is a TypeScript framework for building AI agents. Using Mastra’s LangSmith exporter, you can send traces from your Mastra agents and workflows to LangSmith for debugging, evaluation, and observability. This guide shows you how to integrate Mastra with LangSmith using Mastra’s AI tracing system.

Installation

Install Mastra and the LangSmith exporter:

Setup

  1. Set your LangSmith API key and the selected environment’s tracer_session_name:
    The exporter sends runs to an agent when the agent addressing variables are set, but it attaches scores from Mastra scorers by project name. Set LANGCHAIN_PROJECT to the agent environment’s tracer_session_name so that runs and scores both reach the agent. Do not also set LANGSMITH_AGENT_ID and LANGSMITH_AGENT_ENVIRONMENT: when they are set together with a project name, LangSmith does not trace the calls. If you leave LANGCHAIN_PROJECT unset, traces go to an agent named default.
  2. If you plan to use OpenAI models, also ensure you have an OpenAI API key available at runtime:
  3. In your project directory, create the following project structure and files:

Configure Mastra with the LangSmith exporter

Mastra tracing is configured directly on the Mastra constructor. Add the following to a mastra.ts file:
  • Storage is required for tracing (even when exporting traces externally).
  • The LangSmith exporter reads credentials from environment variables.
  • The deprecated telemetry system is disabled to avoid warnings.
  • No separate instrumentation file is required when running Mastra outside of the Mastra server. For more details, refer to the Mastra docs.

Define an agent

For compatibility, use string-based model identifiers. Add the following code to an agent.ts file:
Mastra will automatically route the model call using your configured API keys and capture traces for each invocation.

Run the agent

  1. Add the following to an index.ts file:
  2. Run your agent:

View traces in LangSmith

After running the agent:
  1. Open the LangSmith UI.
  2. Select your agent, go to Tracing, and select the environment whose tracer_session_name you set in LANGCHAIN_PROJECT.
  3. Locate the trace corresponding to echoAgent.generate.
You’ll be able to inspect:
  • Model inputs and outputs
  • Agent execution steps
  • Timing and error information