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.7.tar.gz (115.8 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.7-py3-none-any.whl (89.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: metricai-0.5.7.tar.gz
  • Upload date:
  • Size: 115.8 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.7.tar.gz
Algorithm Hash digest
SHA256 3c1971d9dbe3d29c1c3684051b7c02af7c40f0897d592b3373374af5fc843434
MD5 6105002ffd458d73999abd59d5e9ce0d
BLAKE2b-256 e6b30be946a3139148aa02e3c42623fce3fd2690d70b541b2a5f63e3a97db20e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: metricai-0.5.7-py3-none-any.whl
  • Upload date:
  • Size: 89.3 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.7-py3-none-any.whl
Algorithm Hash digest
SHA256 d22003534e7e3b750fa0bb194cb2dfa0bf740dd7484217ac7f9e03322b171a0b
MD5 c9605cf85191df47f279309c5999dd88
BLAKE2b-256 365886f1c5929b92594e9f7d0855a9822bde0735c4401758ec7609f01ae67c73

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