Skip to main content

AgentMetrics observability integration for LangChain agents

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agentmetrics_langchain-0.2.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

agentmetrics_langchain-0.2.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file agentmetrics_langchain-0.2.0.tar.gz.

File metadata

  • Download URL: agentmetrics_langchain-0.2.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for agentmetrics_langchain-0.2.0.tar.gz
Algorithm Hash digest
SHA256 66ddabf0ba7fb82ce118cb36772f07278081964c39b89480036f3895502992fa
MD5 e7c7cef3f314d22e023c35baac121bf4
BLAKE2b-256 0965357a9ae455276536228432bb765bf6f149782f3513ad88b4dfd56a8877aa

See more details on using hashes here.

File details

Details for the file agentmetrics_langchain-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for agentmetrics_langchain-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8508e5f191f28ffa5750d9bad2ca91659739b6652d5ae2761352ad1a6cb045d4
MD5 8a85050cc22882e152810dc45a4f8671
BLAKE2b-256 b21adca6bfa3133acee424cb05a4d7d7a3cfce5734167d4e9fe3a372c66483ef

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page