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-tracing 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

Attach the Respan LangChain callback to the runnable config for each chain, model, tool, retriever, or agent run you want traced.

1import os
2from contextlib import suppress
3
4from langchain_core.language_models.fake_chat_models import FakeListChatModel
5from langchain_core.messages import HumanMessage, SystemMessage
6from respan_instrumentation_langchain import add_respan_callback
7from respan_tracing import RespanTelemetry
8
9telemetry = RespanTelemetry(
10 app_name="langchain-quickstart",
11 api_key=os.environ["RESPAN_API_KEY"],
12 base_url=os.getenv("RESPAN_BASE_URL", "https://api.respan.ai/api"),
13 is_auto_instrument=False,
14 is_batching_enabled=False,
15)
16
17model = FakeListChatModel(responses=["Hello from a traced LangChain run."])
18
19try:
20 response = model.invoke(
21 [
22 SystemMessage(content="Reply in one short sentence."),
23 HumanMessage(content="Say hello to Respan tracing."),
24 ],
25 config=add_respan_callback(
26 {
27 "run_name": "hello_world",
28 "tags": ["respan-langchain-example", "quickstart"],
29 "metadata": {"example": "quickstart"},
30 }
31 ),
32 )
33 print(response.content)
34finally:
35 with suppress(Exception):
36 telemetry.flush()

For a provider-backed model, keep the same callback config and swap in your model:

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 callback spans are emitted when you attach the Respan callback config. 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")
28respan.flush()

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)