Skip to Content
Launching the open-source gateway

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.

Set up Respan

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

Paste this into your coding agent:

Help me set up Respan tracing with the LangChain integration. Follow https://www.respan.ai/docs/integrations/langchain.md

Setup

Install packages

Terminal
pip install respan-ai respan-instrumentation-langchain langchain langchain-openai python-dotenv

Set environment variables

Terminal
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.

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.

Python
from langchain_openai import ChatOpenAI from respan import Respan from respan_instrumentation_langchain import LangChainInstrumentor # Activates global LangChain instrumentation — every run is traced automatically. respan = Respan(instrumentations=[LangChainInstrumentor()]) llm = ChatOpenAI(model="gpt-4.1-nano") response = llm.invoke("Say hello in three languages.") print(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.

Python
from langchain_openai import ChatOpenAI from respan_instrumentation_langchain import add_respan_callback llm = ChatOpenAI(model="gpt-4.1-nano") response = llm.invoke( "Say hello in three languages.", config=add_respan_callback( { "run_name": "hello_langchain", "tags": ["langchain", "quickstart"], "metadata": {"framework": "langchain"}, } ), ) print(response.content)

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.

Python
from respan import Respan from respan_instrumentation_langchain import LangChainInstrumentor respan = Respan( instrumentations=[LangChainInstrumentor()], customer_identifier="user_123", metadata={"service": "langchain-api", "version": "1.0.0"}, )

With propagate_attributes

Override per-request using a context scope.

Python
from langchain_openai import ChatOpenAI from respan import Respan, propagate_attributes from respan_instrumentation_langchain import ( LangChainInstrumentor, add_respan_callback, ) respan = Respan(instrumentations=[LangChainInstrumentor()]) llm = ChatOpenAI(model="gpt-4.1-nano") def handle_request(user_id: str, question: str): with propagate_attributes( customer_identifier=user_id, thread_identifier="conv_abc_123", metadata={"plan": "pro"}, ): response = llm.invoke( question, config=add_respan_callback({"run_name": "user_question"}), ) 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.

Python
from langchain_openai import ChatOpenAI from respan import Respan, workflow, task from respan_instrumentation_langchain import ( LangChainInstrumentor, add_respan_callback, ) respan = Respan(instrumentations=[LangChainInstrumentor()]) llm = ChatOpenAI(model="gpt-4.1-nano") @task(name="generate_outline") def outline(topic: str) -> str: return llm.invoke( f"Create a brief outline about: {topic}", config=add_respan_callback({"run_name": "outline_model"}), ).content @workflow(name="content_pipeline") def pipeline(topic: str): plan = outline(topic) response = llm.invoke( f"Write content from this outline: {plan}", config=add_respan_callback({"run_name": "draft_model"}), ) print(response.content) pipeline("Benefits of API gateways")

Examples

Chains

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

Python
from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ChatPromptTemplate from langchain_openai import ChatOpenAI from respan_instrumentation_langchain import add_respan_callback prompt = ChatPromptTemplate.from_messages([ ("system", "Translate the user text to {language}."), ("human", "{text}"), ]) chain = prompt | ChatOpenAI(model="gpt-4.1-nano") | StrOutputParser() result = chain.invoke( {"language": "French", "text": "Hello, Respan."}, config=add_respan_callback({"run_name": "translation_chain"}), ) print(result)

Tools

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

Python
from langchain_core.tools import tool from respan_instrumentation_langchain import add_respan_callback @tool def get_weather(city: str) -> str: """Get the weather for a city.""" return f"It is sunny in {city}." result = get_weather.invoke( {"city": "San Francisco"}, config=add_respan_callback({"run_name": "weather_tool"}), ) print(result)

Streaming

Streaming responses are traced like regular calls.

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

Gateway

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