LangChain (tracing)

LangChain is a framework for building applications with language models. It provides chains, agents, tools, retrievers, and provider integrations. Respan gives you full observability over every chain run, agent step, retriever call, tool call, and LLM generation — and gateway routing through the OpenAI-compatible Respan endpoint.

Create an account at platform.respan.ai and grab an API key.

Run npx @respan/cli setup to set up with your coding agent.

See LangChain gateway setup to route this integration through the Respan gateway.

Setup

1

Install packages

$pip install respan-ai respan-instrumentation-langchain langchain langchain-openai python-dotenv
2

Set environment variables

$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
$export RESPAN_BASE_URL="https://api.respan.ai/api"

RESPAN_API_KEY is used to export traces to Respan. Set OPENAI_API_KEY too when your LangChain run uses a provider-backed model instead of a fake/local model.

3

Initialize and run

Pass LangChainInstrumentor() to Respan(instrumentations=[...]). The instrumentor captures every chain, model, tool, retriever, and agent run.

Python — the instrumentor patches LangChain’s callback manager on init, so every run (chains, agents, graphs, and their nested LLM, tool, and retriever calls) is traced automatically. No per-call setup.

TypeScript — the instrumentor does not patch globally. Attach langchain.addCallback(...) at the outermost .invoke() (your chain, agent, or graph); LangChain propagates it to nested runs. Attaching it only to an inner model yields just that one LLM span.

1from langchain_openai import ChatOpenAI
2from respan import Respan
3from respan_instrumentation_langchain import LangChainInstrumentor
4
5# Activates global LangChain instrumentation — every run is traced automatically.
6respan = Respan(instrumentations=[LangChainInstrumentor()])
7
8llm = ChatOpenAI(model="gpt-4.1-nano")
9response = llm.invoke("Say hello in three languages.")
10print(response.content)

Optionally attach a callback config to label a specific run with a run_name, tags, or metadata. In Python this is only for labeling — tracing already happens through the instrumentor. In TypeScript it is also what activates tracing for the run.

1from langchain_openai import ChatOpenAI
2from respan_instrumentation_langchain import add_respan_callback
3
4llm = ChatOpenAI(model="gpt-4.1-nano")
5response = llm.invoke(
6 "Say hello in three languages.",
7 config=add_respan_callback(
8 {
9 "run_name": "hello_langchain",
10 "tags": ["langchain", "quickstart"],
11 "metadata": {"framework": "langchain"},
12 }
13 ),
14)
15print(response.content)
4

View your trace

Open the Traces page to see your LangChain workflow with chain runs, LLM calls, retriever spans, and tool calls.

Configuration

ParameterTypeDefaultDescription
api_keystr | NoneNoneFalls back to RESPAN_API_KEY env var.
base_urlstr | NoneNoneFalls back to RESPAN_BASE_URL env var.
instrumentationslist[]Plugin instrumentations to activate, e.g. LangChainInstrumentor().
include_contentboolTrueIncludes inputs and outputs on LangChain spans.
include_metadataboolTrueIncludes LangChain tags, metadata, and serialized runnable details.
customer_identifierstr | NoneNoneDefault customer identifier for all spans.
metadatadict | NoneNoneDefault metadata attached to all spans.
environmentstr | NoneNoneEnvironment tag, e.g. "production".

Attributes

In Respan()

Set defaults at initialization — these apply to all spans.

1from respan import Respan
2from respan_instrumentation_langchain import LangChainInstrumentor
3
4respan = Respan(
5 instrumentations=[LangChainInstrumentor()],
6 customer_identifier="user_123",
7 metadata={"service": "langchain-api", "version": "1.0.0"},
8)

With propagate_attributes

Override per-request using a context scope.

1from langchain_openai import ChatOpenAI
2from respan import Respan, propagate_attributes
3from respan_instrumentation_langchain import (
4 LangChainInstrumentor,
5 add_respan_callback,
6)
7
8respan = Respan(instrumentations=[LangChainInstrumentor()])
9llm = ChatOpenAI(model="gpt-4.1-nano")
10
11def handle_request(user_id: str, question: str):
12 with propagate_attributes(
13 customer_identifier=user_id,
14 thread_identifier="conv_abc_123",
15 metadata={"plan": "pro"},
16 ):
17 response = llm.invoke(
18 question,
19 config=add_respan_callback({"run_name": "user_question"}),
20 )
21 print(response.content)
AttributeTypeDescription
customer_identifierstrIdentifies the end user in Respan analytics.
thread_identifierstrGroups related messages into a conversation.
metadatadictCustom key-value pairs. Merged with default metadata.

Decorators (optional)

Decorators are not required. LangChain spans are emitted automatically by the instrumentor in Python, and when you attach the Respan callback config in TypeScript. Use @workflow and @task (Python) or withWorkflow and withTask (TypeScript) to add structure when you want to group related runs into a named workflow with nested tasks.

1from langchain_openai import ChatOpenAI
2from respan import Respan, workflow, task
3from respan_instrumentation_langchain import (
4 LangChainInstrumentor,
5 add_respan_callback,
6)
7
8respan = Respan(instrumentations=[LangChainInstrumentor()])
9llm = ChatOpenAI(model="gpt-4.1-nano")
10
11@task(name="generate_outline")
12def outline(topic: str) -> str:
13 return llm.invoke(
14 f"Create a brief outline about: {topic}",
15 config=add_respan_callback({"run_name": "outline_model"}),
16 ).content
17
18@workflow(name="content_pipeline")
19def pipeline(topic: str):
20 plan = outline(topic)
21 response = llm.invoke(
22 f"Write content from this outline: {plan}",
23 config=add_respan_callback({"run_name": "draft_model"}),
24 )
25 print(response.content)
26
27pipeline("Benefits of API gateways")

Examples

Chains

Chains are traced as workflow/task spans with nested LLM and tool spans.

1from langchain_core.output_parsers import StrOutputParser
2from langchain_core.prompts import ChatPromptTemplate
3from langchain_openai import ChatOpenAI
4from respan_instrumentation_langchain import add_respan_callback
5
6prompt = ChatPromptTemplate.from_messages([
7 ("system", "Translate the user text to {language}."),
8 ("human", "{text}"),
9])
10chain = prompt | ChatOpenAI(model="gpt-4.1-nano") | StrOutputParser()
11
12result = chain.invoke(
13 {"language": "French", "text": "Hello, Respan."},
14 config=add_respan_callback({"run_name": "translation_chain"}),
15)
16print(result)

Tools

Tool calls are captured with tool name, arguments, result, and timing.

1from langchain_core.tools import tool
2from respan_instrumentation_langchain import add_respan_callback
3
4@tool
5def get_weather(city: str) -> str:
6 """Get the weather for a city."""
7 return f"It is sunny in {city}."
8
9result = get_weather.invoke(
10 {"city": "San Francisco"},
11 config=add_respan_callback({"run_name": "weather_tool"}),
12)
13print(result)

Streaming

Streaming responses are traced like regular calls.

1for chunk in llm.stream(
2 "Write a haiku about Python.",
3 config=add_respan_callback({"run_name": "streaming_model"}),
4):
5 print(chunk.content, end="", flush=True)