openai.chat.completions.create call at the point where LangChain invokes it, using a custom LangChain callback or a thin wrapper around the OpenAI client. This gives you full span visibility without touching your chain or agent logic.
How the pattern works
LangChain calls OpenAI internally. TracePilot’swrapOpenAI intercepts that call, captures inputs, outputs, tokens, and latency as a span, then returns the original result untouched. LangChain never knows TracePilot is there.
TracePilot wraps OpenAI calls — it does not wrap LangChain itself. Your chains, agents, and tools continue to work exactly as before.
Setup
1
Install dependencies
2
Create a traced OpenAI client
Extend the LangChain
ChatOpenAI class to intercept completions calls before they reach OpenAI. Pass tp.wrapOpenAI as the call executor inside the _generate override.3
Build your chain with the traced model
Use
TracedChatOpenAI anywhere you’d use ChatOpenAI. Pass stepOrder to match LangChain’s chain step numbering so spans appear in the correct order in the dashboard.Multi-step chains
When your chain has multiple LLM steps, passstepOrder to each TracedChatOpenAI instance and use parentSpanId to link them into a tree.
What you’ll see in the dashboard
EachwrapOpenAI call creates one span. For a two-step chain, you get a parent span for Step 1 and a child span for Step 2 nested beneath it. Click any span to inspect the exact messages sent to OpenAI, the response, token usage, latency, and cost.