Skip to main content

A short description of your package

Project description

a2a-llm-tracker

a2a-llm-tracker is a Python package that helps AI agents and applications track LLM usage and cost from a single place, across providers like OpenAI, Gemini, Anthropic, and others.

It is designed for agent-to-agent (A2A) systems where:

  • multiple agents make LLM calls
  • multiple providers are used
  • usage and cost need to be tracked centrally
  • streaming, async, and sync calls must all be supported

The package is LiteLLM-first, giving you multi-provider support with minimal integration effort.


Why a2a-llm-tracker?

LLM providers differ in:

  • SDKs and APIs
  • tokenization
  • pricing models
  • usage reporting (especially for streaming)

a2a-llm-tracker solves this by:

  • wrapping LLM calls instead of guessing usage
  • normalizing provider-reported usage into a single schema
  • computing cost using configurable pricing
  • attaching agent / user / session context
  • writing usage events to pluggable storage backends

Exact cost is recorded only when providers report usage.
This package does not fabricate billing data.


Features

  • ✅ Multi-provider support via LiteLLM
  • ✅ Sync, async, and streaming calls
  • ✅ Exact token usage when available
  • ✅ Cost calculation with user-defined pricing
  • ✅ Context propagation for agents and sessions
  • ✅ JSONL and SQLite sinks
  • ✅ No heavy work on import
  • ✅ No vendor lock-in

Installation

pip install a2a-llm-tracker[litellm]

Quickstart

1️⃣ Set your API key

Example for OpenAI:

export OPENAI_API_KEY=sk-xxxxxxxx

Import

from a2a_llm_tracker import Meter, PricingRegistry, meter_context

Create a tracker

from a2a_llm_tracker import Meter, PricingRegistry
from a2a_llm_tracker.sinks.jsonl import JSONLSink

pricing = PricingRegistry()
pricing.set_price(
    provider="openai",
    model="openai/gpt-4.1",
    input_per_million=2.0,
    output_per_million=8.0,
)

meter = Meter(
    pricing=pricing,
    sinks=[JSONLSink("usage.jsonl")],
    project="my-a2a-system",
)


Wrap Litellm

from a2a_llm_tracker.integrations.litellm import LiteLLM

llm = LiteLLM(meter=meter)

Use The package Sync

response = llm.completion(
    model="openai/gpt-4.1",
    messages=[
        {"role": "user", "content": "Say hello in one sentence."}
    ],
)

print(response)

Use package for Sync streaming

for chunk in llm.completion(
    model="openai/gpt-4.1",
    messages=[{"role": "user", "content": "Write a short poem."}],
    stream=True,
):
    print(chunk, end="", flush=True)


Streaming output is yielded as usual

Usage is recorded after the stream finishes

If the provider does not return usage for streams, accuracy is marked as unknown

Async Non Stereaming

response = await llm.acompletion(
    model="openai/gpt-4.1",
    messages=[{"role": "user", "content": "Async hello!"}],
)

Async Steraming

stream = await llm.acompletion(
    model="openai/gpt-4.1",
    messages=[{"role": "user", "content": "Stream async output"}],
    stream=True,
)

async for chunk in stream:
    print(chunk, end="", flush=True)

Agent and Session Context

from a2a_llm_tracker import meter_context

with meter_context(
    agent_id="planner-agent",
    session_id="session-123",
    user_id="user-456",
):
    llm.completion(
        model="openai/gpt-4.1",
        messages=[{"role": "user", "content": "Plan my day"}],
    )

Pricing is fully user controlled

Pricing Model

Pricing is fully user-controlled.

pricing.set_price(
    provider="openai",
    model="openai/gpt-4.1",
    input_per_million=2.0,
    output_per_million=8.0,
)




This supports:

enterprise pricing

price changes over time

multiple vendors with different rates

This package does not

What This Package Does NOT Do

❌ Guess exact billing from raw text

❌ Replace provider SDKs

❌ Upload data anywhere automatically

❌ Require a backend or SaaS

Building this project

use the venv to build the environment

python -m venv .venv

pip install -e .

To build the project

python -m build 

To publish the project

python -m twine upload dist/*

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

a2a_llm_tracker-0.0.2.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

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

a2a_llm_tracker-0.0.2-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file a2a_llm_tracker-0.0.2.tar.gz.

File metadata

  • Download URL: a2a_llm_tracker-0.0.2.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for a2a_llm_tracker-0.0.2.tar.gz
Algorithm Hash digest
SHA256 17a9c9cfcf3c538f04bed1e571aa733e190fad5caf2569ce943c6ea4a241a8a5
MD5 c645c10137ac13220722d98250c056fd
BLAKE2b-256 4c607eb13b6f169ee480bde7fcf92275fa654bd6c6182967c8653c50380b75f0

See more details on using hashes here.

File details

Details for the file a2a_llm_tracker-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for a2a_llm_tracker-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4d9715352d58eb0260665d05555acedfea6be1f38c264608936951055aa94cbb
MD5 01f14f6933cbf1795f5b57b64c4b44df
BLAKE2b-256 d3dfadd67dc0555ff36ab4d2383e820b9e9def0045421be72f75d0e55c6484a1

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