Skip to main content

AgentMetrics observability integration for LlamaIndex agents

Project description

agentmetrics-llamaindex

PyPI License: MIT

AgentMetrics integration for LlamaIndex. Call instrument() once and every agent run and query engine response reports back to your dashboard showing latency, cost, token counts, tool calls, and errors, via LlamaIndex's native instrumentation API with zero changes to your agent code.


Install

pip install agentmetrics-llamaindex

Quickstart

from agentmetrics_llamaindex import instrument

# Register on the global root dispatcher once at startup
span_handler = instrument(
    agent_id="my-llamaindex-agent",
    base_url="http://localhost:8099",
)

# Run your agents and query engines as normal
response = agent.chat("Summarize this document")

span_handler.flush()

API

instrument(agent_id, base_url)

Registers AgentMetricsSpanHandler and AgentMetricsEventHandler on the global LlamaIndex root dispatcher. Returns the span handler for flushing.

Parameter Default Description
agent_id "llamaindex-agent" Fallback label if the agent has no name attribute
base_url "http://localhost:8099" AgentMetrics server address

AgentMetricsSpanHandler

LlamaIndex BaseSpanHandler that tracks top-level agent/engine spans. Emits a run summary on span completion or error.

AgentMetricsEventHandler

LlamaIndex BaseEventHandler that accumulates token counts and tool calls from LLMChatEndEvent, LLMCompletionEndEvent, and AgentToolCallEvent.

.flush(timeout=10.0)

Blocks until all in-flight HTTP requests complete.


What gets tracked

Each top-level agent or query engine span emits one event to /v1/events:

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

The handler detects top-level spans by checking whether the span has no parent and whether the owning instance is an agent, engine, runner, or query object.


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_llamaindex-0.2.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

agentmetrics_llamaindex-0.2.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for agentmetrics_llamaindex-0.2.0.tar.gz
Algorithm Hash digest
SHA256 69a06e12e2bbc6d8c495e9db4dd655feb5f9c4766a13a56a5eefe5193476a7f9
MD5 0fa77dcae5345915d689f70abd0523ec
BLAKE2b-256 3b7c360bb2b896b45d80ccd455d0420a36a76bec827a07ba1bb76a139865a320

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for agentmetrics_llamaindex-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f784f9599b3c8cb388f45a7a6fe35bf6c33b7f7db38af78605785c785d65dc26
MD5 2fb69e3066827db0cbea661806bc8aab
BLAKE2b-256 94a03a58efa6ebf05b105f4da7409f8f121da6c4dbd321dbf1a549d848aadb2e

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