Integration

LangChain observability — drop-in. ~5 minutes.

Trace every chain step, tool call, retry, and agent decision in your LangChain agents. No code rewrites. OpenTelemetry-native spans that fan out to whatever else you run.

What we capture for LangChain

  • Every LLM call: model, prompt, completion, tokens, latency
  • Every tool invocation: name, args, return value, exception
  • Every chain step: inputs and outputs at the runnable boundary
  • Retry attempts and back-off windows
  • Agent decisions: which tool, why, what context the LLM saw
  • Cost per trace and rolling p50 / p95 latency per chain

Install

shell
# Python
pip install trefur-observe langchain openai

# Set your key in the environment (free tier signup at trefur.com)
export TREFUR_API_KEY=trf_obs_REPLACE_ME

Instrument

agent.py
from trefur_observe import TrefurObserve, patch_langchain
from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.tools import tool

obs = TrefurObserve.init(api_key="trf_obs_...")  # or read from TREFUR_API_KEY env var
patch_langchain(obs)

@tool
def get_account(account_id: str) -> dict:
    """Return account details for a given account_id."""
    return {"id": account_id, "tier": "pro", "balance_cents": 4200}

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a support agent. Use tools to answer."),
    ("user", "{input}"),
    ("placeholder", "{agent_scratchpad}"),
])
llm = ChatOpenAI(model="gpt-4o")
agent = create_openai_tools_agent(llm, [get_account], prompt)
executor = AgentExecutor(agent=agent, tools=[get_account])

executor.invoke({"input": "What's the balance on account A-42?"})
# Open https://app.trefur.com → trace appears within seconds.

What a LangChain trace looks like

trace · support-l1 · 1.8s · $0.00144 spans
$ AgentExecutor.invoke [root] 1820ms
$ llm ChatOpenAI gpt-4o in 412 / out 86 210ms
$ tool get_account('A-42') {tier: pro, ...} 90ms
$ llm ChatOpenAI gpt-4o in 530 / out 124 280ms
$ return 'Balance is $42.00 on tier pro.'

Three common LangChain pain points, and what Trefur shows you

Silent tool failures

LangChain swallows tool exceptions and asks the LLM to keep going. Trefur surfaces the failed tool call, the exception, and the prompt the LLM saw next — so you can see why the agent kept going.

Unbounded retry loops

When a tool keeps returning malformed JSON, the agent loops until the iteration limit hits. Trefur charts iteration count over time and alerts when a trace exceeds your normal envelope.

Token spikes you can't account for

Trefur attributes tokens to the specific chain step that emitted them — so when costs jump, you know whether it was the LLM thinking out loud or the tool output blowing up the context window.

FAQ

Do I have to change my agent code?+

No. Call TrefurObserve.init() and patch_langchain(obs) at startup — Trefur registers as a LangChain callback. Existing AgentExecutor / chain code runs unchanged.

What about streaming responses?+

Streamed tokens are captured as a single span with the full completion. We do not buffer the stream to the user — your latency is unchanged.

Does it work with LCEL / runnables?+

Yes. Every runnable invocation, including chained .pipe() calls, emits a span. The trace shows the full topology.

What about LangGraph?+

LangGraph builds on the same callback surface. The graph state transitions show up as named spans inside the agent trace.

Ship LangChain agents you can debug.

Free tier. No card required. First trace in under five minutes.