Skip to main content
Agents built with LangChain, LangGraph, or Deep Agents support OpenTelemetry-based tracing. Instead of sending traces directly to LangSmith, you can route them through an OpenTelemetry collector you control, apply redaction rules to strip sensitive fields, and forward the sanitized traces to LangSmith. Traces flow from your agent to the collector over OTLP/HTTP. The collector runs a transform processor that redacts sensitive span attributes, such as prompt inputs and model completions, before forwarding the sanitized spans to the LangSmith API. You can send traces to the collector in two ways: through the LangChain, LangGraph, or Deep Agents integration, or by configuring the OpenTelemetry SDK directly.
In an agent-based workspace, create an agent once before sending traces. Use agent addressing to select its identifier and environment. If an integration requires project_name, use the selected environment’s tracer_session_name as a compatibility value.

Prerequisites

Both approaches require the following environment variables. Set OTEL_EXPORTER_OTLP_ENDPOINT to the address of your collector:
LANGSMITH_AGENT_ID and LANGSMITH_AGENT_ENVIRONMENT name the agent and environment the traces belong to. For more information, see Log traces to an agent.

Configure the collector

Both approaches also require an OpenTelemetry collector running as an intermediary between your agent and LangSmith. The following configuration sets up an OTLP receiver on port 4318, a transform processor that redacts the gen_ai.prompt and gen_ai.completion span attributes, and an exporter that forwards the sanitized traces to the LangSmith API:
The collector reads LANGSMITH_API_KEY, LANGSMITH_AGENT_ID, and LANGSMITH_AGENT_ENVIRONMENT from its own environment, not from your application’s. Set them where the collector runs, along with LANGSMITH_TENANT_ID if your API key is not scoped to a specific workspace.

Send traces to an agent

The collector, not your application, decides which agent and environment receive the traces. The collector sends its own headers to LangSmith and does not forward the headers your application sends by default. The otlphttp/langsmith exporter in the preceding configuration sets both agent headers:
  • Langsmith-Agent-Id: The identifier of the agent. It must be 1 to 63 lowercase letters, digits, or hyphens, start with a letter, and end with a letter or digit.
  • Langsmith-Agent-Environment: The environment the traces belong to. One of local, development, staging, or production.
Send both agent headers or neither. LangSmith rejects a request that sets one without the other, or that sets either alongside Langsmith-Project, with a 400 error. The headers apply to every span the exporter sends. To send traces to more than one agent or environment, define one exporter for each, and route each agent’s spans to its exporter. Span attributes, including gen_ai.agent.name, do not send traces to an agent. To learn more about sending traces to an agent, see Log traces to an agent.

Trace with LangChain, LangGraph, or Deep Agents

Use this approach if your agent already uses LangChain, LangGraph, or Deep Agents. The tracing integration handles span creation automatically based on your environment variables, so no additional instrumentation code is required:

Trace with the OpenTelemetry SDK directly

Use this approach if you need programmatic control over the tracer provider and exporter, such as which span processors run or where the exporter sends spans. You configure the provider explicitly in code rather than relying on environment variables alone. The collector’s exporter, not your code, decides which agent receives the traces:
If you prefer to redact sensitive data without routing through a collector, see Prevent logging of sensitive data in traces.