Skip to main content

agentmetrics-langchain

PyPI License: MIT

AgentMetrics integration for LangChain. Pass one callback to any chain or agent .invoke() call and every run reports back to your dashboard showing latency, cost, token counts, tool calls, and errors, with no changes to your agent logic.


Install

pip install agentmetrics-langchain

Quickstart

from agentmetrics_langchain import AgentMetricsCallback

cb = AgentMetricsCallback(
    agent_id="my-langchain-agent",
    base_url="http://localhost:8099",
)

result = agent.invoke(
    {"input": "What is the weather in Paris?"},
    config={"callbacks": [cb]},
)

cb.flush()

API

AgentMetricsCallback(agent_id, base_url)

Parameter Default Description
agent_id "langchain-agent" Label shown in the dashboard
base_url "http://localhost:8099" AgentMetrics server address

The callback is a BaseCallbackHandler. Pass it via config={"callbacks": [cb]} on any chain or agent .invoke() call. It tracks the top-level chain only, with nested sub-chains aggregated into the same run.

Supports both OpenAI-style and Anthropic-style token counting from usage_metadata and llm_output.

.flush(timeout=10.0)

Blocks until all in-flight HTTP requests complete. Call before process exit in scripts.


What gets tracked

Each top-level chain invocation emits one event to /v1/events on completion or error:

Field Description
status success or failed
duration_ms Wall-clock chain duration
input_tokens / output_tokens Aggregated across all LLM calls in the chain
cache_read_tokens / cache_write_tokens Cache token counts (Anthropic)
llm_calls Number of LLM requests in the chain
tool_calls / tool_errors Tool usage counts
tool_names Set of tools invoked
model Model name from the first LLM call
estimated_cost_usd Computed from token counts and model pricing
error First 500 chars of the error message on failure

LangGraph

The callback works with LangGraph graphs the same way:

from langgraph.graph import StateGraph
from agentmetrics_langchain import AgentMetricsCallback

cb  = AgentMetricsCallback(base_url="http://localhost:8099")
app = build_graph().compile()

result = app.invoke(state, config={"callbacks": [cb]})

License

MIT

Metadata

Release files for agentmetrics-langchain 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for agentmetrics-langchain 0.2.0
File Size Uploaded
agentmetrics_langchain-0.2.0.tar.gz 5.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for agentmetrics-langchain 0.2.0
File Interpreter ABI Platform
agentmetrics_langchain-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.5 kB

Release files / agentmetrics_langchain-0.2.0.tar.gz

Download URL agentmetrics_langchain-0.2.0.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
66ddabf0ba7fb82ce118cb36772f07278081964c39b89480036f3895502992fa
BLAKE2b-256 checksum
How to use checksums
0965357a9ae455276536228432bb765bf6f149782f3513ad88b4dfd56a8877aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / agentmetrics_langchain-0.2.0-py3-none-any.whl

Download URL agentmetrics_langchain-0.2.0-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8508e5f191f28ffa5750d9bad2ca91659739b6652d5ae2761352ad1a6cb045d4
BLAKE2b-256 checksum
How to use checksums
b21adca6bfa3133acee424cb05a4d7d7a3cfce5734167d4e9fe3a372c66483ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page