Skip to main content

Python SDK for MetricAI — AI billing and metering proxy

Project description

metricai

Python SDK for MetricAI — AI billing and metering proxy.

Quick reference

  • Config / headers: MetricAIConfig, MetricAI(...).headers(agent_id, user_id, ...)
  • Native OpenAI / Anthropic SDKs: openai_sdk(), anthropic_sdk(), gemini_sdk(), grok_sdk()
  • Shared session id for SDK calls: proxy_sdk_session(...)MetricAIProxyClientSession
  • Telemetry (existing): track() / MetricAISession — unchanged
  • LangChain / LangGraph / CrewAI: metricai.integrations (lazy imports for optional deps)
  • Standalone workflow: from metricai.workflow import MetricAIAgent, MetricAIPipeline

Margin-ready integration checklist (under 30 minutes)

For live per-agent and per-end-user attribution in the dashboard, include these on every SDK call:

  • agent_id: stable agent identifier (for example support-agent-v1)
  • user_id: end-user/customer identifier (for example acct_42)

For explicit margin reporting, include revenue metadata on tracked events:

  • Prefer sending revenue_usd in track(..., extra={...}) when available.
  • If your app uses outcome billing, billable_amount_inr is derived from outcome and can be used server-side as revenue.

Verification flow:

  1. Send at least one billable event with agent_id + user_id.
  2. Open dashboard Cost & Margin attribution view.
  3. Confirm non-empty rows under both by-agent and by-end-user tables.

Examples live under examples/ (e.g. byok_openai_sdk.py, standalone_workflow.py).

Official SDK URL paths (via MetricAI proxy)

The hosted API mirrors what the OpenAI / Anthropic clients append to base_url:

SDK base_url ends with Effective POST path
openai.OpenAI /v1/proxy/openai /v1/proxy/openai/chat/completions
anthropic.Anthropic /v1/proxy/claude /v1/proxy/claude/v1/messages
openai.OpenAI (Gemini route) /v1/proxy/gemini /v1/proxy/gemini/chat/completions
openai.OpenAI (Grok route) /v1/proxy/grok /v1/proxy/grok/chat/completions

Legacy single-segment POSTs (/v1/proxy/openai, /claude, /gemini, /grok) still work.

Response shape: MetricAIProxy uses the .../chat/completions path for OpenAI-shaped providers so the HTTP body matches the native OpenAI chat.completion JSON (what openai.OpenAI expects). Posts to the legacy /v1/proxy/openai path without /chat/completions return the MetricAI metering envelope unless you send header X-MetricAI-Response-Format: metricai on the chat path (optional).

Programmatic usage: GET /v1/usage?window=7d (same auth as the proxy: JWT or X-MetricAI-API-Key) returns dashboard-aligned summaries; see backend docs/PILOT_DATA_PROCESSING.md.

Use examples/provider_sdk_check.py to smoke-test providers when keys are set.

Most examples honor METRICAI_EXAMPLE_PROVIDER (openai | claude | gemini | grok) and shared env documented in examples/_provider_env.py.

See METRICAI_SDK.md for full developer documentation (patterns, headers, sessions, governance v1, changelog). \x00

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

metricai-0.5.6.tar.gz (113.4 kB view details)

Uploaded Source

Built Distribution

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

metricai-0.5.6-py3-none-any.whl (87.5 kB view details)

Uploaded Python 3

File details

Details for the file metricai-0.5.6.tar.gz.

File metadata

  • Download URL: metricai-0.5.6.tar.gz
  • Upload date:
  • Size: 113.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for metricai-0.5.6.tar.gz
Algorithm Hash digest
SHA256 51282bbf82633ed4e45445617066e2a43c1fd7595fbccd02d1c64ab0b860c18f
MD5 e5d0ac5fcb6b8ba383f24080d99da337
BLAKE2b-256 cd51ad8e9f926b72801492b27908cf49fee4e463c893cd8fc42f8cbbf57abfbc

See more details on using hashes here.

File details

Details for the file metricai-0.5.6-py3-none-any.whl.

File metadata

  • Download URL: metricai-0.5.6-py3-none-any.whl
  • Upload date:
  • Size: 87.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for metricai-0.5.6-py3-none-any.whl
Algorithm Hash digest
SHA256 86cc0188798814b6f811c5e958d00c34664d925e4cb69ecc6df0b0a938c09ef7
MD5 147d75c86be0b3b8cbc7d4c8f5a5ea03
BLAKE2b-256 be2bdd2122a87abb3460d2991339437f08875b3194db81d632a4a59201a03508

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