LangChain

Track every LangChain agent run with cost, latency, token usage, and tool call data using the AgentMetrics callback handler.

Requirements

  • Python 3.10 or later
  • langchain-core 0.2 or later

1. Install

Shell
pip install agentmetrics-langchain

2. Add the callback

Pass AgentMetricsCallback in the callbacks list when invoking your agent or chain.

Python
import os
from agentmetrics_langchain import AgentMetricsCallback

cb = AgentMetricsCallback(
    api_key=os.environ["AGENTMETRICS_API_KEY"],
    agent_id="my-langchain-agent",
    base_url="http://localhost:8099",
)

result = agent.invoke(
    {"input": "summarize this document"},
    config={"callbacks": [cb]},
)

3. Flush before exit (optional)

For short-lived scripts, call flush() to wait for all in-flight requests to complete before the process exits.

Python
cb.flush()

What gets tracked

  • Duration from chain start to chain end
  • Success or failure (exceptions are recorded and re-raised)
  • Input and output token counts, including cache read and write tokens
  • Cost (estimated from model pricing tables)
  • Tool call count, tool names, and tool errors
  • Model name (extracted from LLM response metadata)

Configuration

ParameterDefaultDescription
api_keyrequiredYour AgentMetrics API key
agent_id"langchain-agent"Label shown in the dashboard
base_url"http://localhost:8099"AgentMetrics backend URL