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

Uploaded Python 3

File details

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

File metadata

  • Download URL: metricai-0.5.1.tar.gz
  • Upload date:
  • Size: 97.1 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.1.tar.gz
Algorithm Hash digest
SHA256 5a86cee91bd64070f4bedfed54688a54a9253cce071e7b070f94192268d23950
MD5 9de7e7b38a229861ec99b6f7c0e2fd8f
BLAKE2b-256 c582367d559d2c379ec546e4f8fc784c14c8c2636abd74835074c04e7bcae16a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: metricai-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 81.9 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 070ff62f65f538c1b0e9db23ba9ceb360517e85ae19727aa4f92e62af107c120
MD5 43332e3d2f777f11c68a947719b639c7
BLAKE2b-256 41f8df5832f39219c0810426189043bb1748f27a79f8b3d5113c1026c812e060

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