Tutorial
2026-03-125 min read

How to Instrument Your LangChain Agent in 3 Minutes

Step-by-step tutorial for adding observability to your LangChain agents with code snippets and trace viewer output.

Share:

Why Instrument Your LangChain Agent?

You've built a LangChain agent. It works — most of the time. But when it breaks, you have no idea why.

Maybe:

  • The agent picked the wrong tool
  • The LLM response was nonsensical
  • It got stuck in a loop
  • Costs spiraled out of control
  • This guide shows you how to add full observability in 3 minutes.

    Step 1: Install the SDK

    ```bash

    pip install trefur

    ```

    Step 2: Initialize Trefur

    Add a few lines to your agent setup:

    ```python

    from trefur_observe import TrefurObserve

    # Initialize with your API key

    trefur = TrefurObserve(api_key="your-api-key")

    trefur.instrument()

    ```

    Step 3: Run Your Agent

    That's it. Run your LangChain agent as normal — Trefur captures everything:

    ```python

    from langchain.agents import AgentExecutor

    # Your existing agent — no changes needed

    agent = AgentExecutor.from_agent_and_tools(...)

    result = agent.invoke({"input": "What's our refund policy?"})

    ```

    What You Get

    Once instrumented, you'll see in the Trefur dashboard:

  • Trace Viewer — Every decision the agent makes, visualized
  • Tool Selection — Which tools were called, in what order
  • Token Usage — Exact token counts per call
  • Latency — How long each step took
  • Cost Breakdown — Exactly what each call cost
  • Example Trace Output

    Here's what a typical trace looks like:

    ```

    🤖 Agent: Reasoning about user query...

    🔧 Tool: search_knowledge_base(query="refund policy")

    📝 LLM: Generating response...

    ✅ Completed in 2.3s | $0.0042

    ```

    Finding Bugs Fast

    Let's say your agent is returning wrong answers. With Trefur:

  • Find the failed trace in the dashboard
  • Click on each step to see what the agent was thinking
  • Identify exactly where it went wrong
  • Fix and redeploy
  • Next Steps

  • Add Trefur to your CI/CD for production monitoring
  • Set up alerts for high costs or errors
  • Share traces with your team
  • Get started at [app.trefur.com](https://app.trefur.com).

    Ready to put this into practice?

    Start tracing your AI agents in 5 minutes with Trefur Observe.