Skip to main content

agentmetrics - Python SDK

PyPI Python 3.9+ License: MIT

AgentMetrics Python SDK. Add @agentmetrics.track to any agent function and every run reports back to your dashboard showing latency, cost, token usage, tool calls, and failures, self-hosted with no account required.


Install

pip install agentmetrics

Requires Python 3.9 or later.


Quickstart

import agentmetrics

agentmetrics.configure(base_url="http://localhost:8099")
agentmetrics.instrument()  # auto-patches OpenAI, Anthropic, LiteLLM, and more

@agentmetrics.track(agent_id="my-agent")
def run(task: str) -> str:
    answer = call_llm(task)
    return answer

Every call to run() reports to your dashboard showing duration, cost, token usage, tool calls, and when it failed.


API

agentmetrics.configure()

Call once at startup before any @track decorators execute.

agentmetrics.configure(
    base_url="http://localhost:8099",  # AgentMetrics server address
    environment="production",          # optional, tags every run
    sample_rate=1.0,                   # optional, 0.0 to 1.0
    batch_size=20,                     # optional, events per batch
    flush_interval=2.0,                # optional, seconds between flushes
)

agentmetrics.instrument()

Patches installed LLM SDKs to auto-capture token counts and model names on every call, and is safe to call multiple times.

agentmetrics.instrument()

Supported: OpenAI (+ Azure, Groq, Together AI) · Anthropic · LiteLLM · Google Gemini · Cohere · Mistral · LangChain / LangGraph / CrewAI · LlamaIndex

@agentmetrics.track()

Decorator for sync and async agent functions.

@agentmetrics.track(agent_id="my-agent", metadata={"env": "prod"})
def run(task: str) -> str:
    return call_llm(task)

@agentmetrics.track(agent_id="async-agent")
async def run_async(task: str) -> str:
    return await call_llm_async(task)
Parameter Type Description
agent_id str Identifier shown in the dashboard
metadata dict Optional key-value pairs attached to every run

agentmetrics.step()

Context manager for timing a named phase within a tracked agent.

@agentmetrics.track(agent_id="pipeline")
def run(query: str) -> str:
    with agentmetrics.step("retrieve"):
        docs = vector_search(query)
    with agentmetrics.step("generate"):
        return call_llm(query, docs)

agentmetrics.tool()

Context manager for tracking individual tool calls.

@agentmetrics.track(agent_id="research-agent")
def run(query: str) -> str:
    with agentmetrics.tool("web_search"):
        results = web_search(query)
    return summarize(results)

agentmetrics.score()

Attaches a named evaluation score to the current run. Call from inside a @track function.

@agentmetrics.track(agent_id="my-agent")
def run(task: str) -> str:
    answer = call_llm(task)
    agentmetrics.score("relevance", 0.92)
    return answer

agentmetrics.flush()

Blocks until all queued events are sent. Call before process exit in scripts.

agentmetrics.flush(timeout=10.0)

agentmetrics.trace_id

Returns the active trace ID from inside a tracked function.

@agentmetrics.track(agent_id="my-agent")
def run(task: str) -> str:
    print(agentmetrics.trace_id)  # e.g. "a3f1c2d4-..."
    return call_llm(task)

License

MIT

Download files

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

Source Distribution

agentmetrics-0.2.0.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

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

agentmetrics-0.2.0-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: agentmetrics-0.2.0.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for agentmetrics-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d2f24605bf78a27e384706591c3d95ebb033ee53c521f42824b41962d83c7c5a
MD5 b7e23445ca79cd5d9252dc6f5b59c557
BLAKE2b-256 dce93125767ef4a0a56d6c7b822bffb8fdc0d7ad5dd7107ef852e083a9a379a0

See more details on using hashes here.

Provenance

The following attestation bundles were made for agentmetrics-0.2.0.tar.gz:

Publisher: publish-packages.yml on andalabx/agentmetrics

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: agentmetrics-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 19.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for agentmetrics-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 840f565ab301dcc6a5ad1f4b5af998fc637c1303a6ad9df086a9a7c0a1b92960
MD5 67e37b4ac92f34908a8b4863e101f484
BLAKE2b-256 452e4c328c5cb58ef750f6da67fc40934d0fe4c70f029737ee048e5d0519c985

See more details on using hashes here.

Provenance

The following attestation bundles were made for agentmetrics-0.2.0-py3-none-any.whl:

Publisher: publish-packages.yml on andalabx/agentmetrics

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Supported by

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