Skip to main content
The Agent SDK is the drop-in instrumentation layer for agent billing: point your existing OpenAI client at the Macropay proxy to capture model cost automatically, then report what the agent did and achieved so margin and ROI compute themselves. It ships for both Python (macropay) and TypeScript (@macropay/sdk) with a matching API.
Instrumentation does three things: captures LLM cost (via the proxy base URL), reports signals (activity + outcome), and records non-LLM cost (tools, third-party APIs, human-in-the-loop). Those three feeds power Agentic Margin and value receipts.

Install

Capture LLM cost with one base URL

The Macropay AI proxy is OpenAI-compatible and mounted at /ai/v1. Point any OpenAI client at it, authenticate with an agent-bound proxy key, and every call’s cost is attributed to that agent — no wrapper code. The SDK exposes a helper so you never hand-write the URL.
openai_base_url() / openaiBaseUrl() default to production. Pass sandbox=True (Python) or { sandbox: true } (TypeScript) to target the sandbox, or pass an explicit base URL to override. See LLM inference for the full proxy reference.

AgentInstrumentation

AgentInstrumentation binds one agent (and optionally one customer) once, so every signal and cost call is a one-liner. It wraps the low-level Macropay client.
All methods are async. Pass external_id to dedupe — outcomes are idempotent on it.

Report what the agent did and achieved

Signals are the heart of agent billing. Activity signals are what the agent did; outcome signals are what it achieved — and an outcome carries the value it produced.
Set verified when an outcome is confirmed by a trusted source — a CRM webhook, a payment, a signed contract — rather than self-reported by the agent. The value receipt then splits verified from unverified value, exposing the trust gap to your customer.

Record non-LLM cost

LLM cost is captured automatically through the proxy. Everything else — search APIs, enrichment, human review — you report with record_cost so margin reflects true COGS.

Automatic tool-cost capture (Python)

track_tool (an async context manager) and track_cost (a decorator) record the cost and emit a tool.<name> activity signal — but only when the call succeeds. A tool call that raises was a failure you weren’t charged for, so it never touches margin.

End-to-end: route a call, post an outcome

A minimal SDR-style agent: the LLM call goes through the proxy (cost captured), then the booked meeting is reported as a verified outcome worth $200.

Close the billing loop in one call

setup_billing / setupBilling provisions a signal-scoped meter for the agent and a product with a metered_unit price — so you skip the manual meter to price to product wiring. Bill per activity or per outcome.
Outcome-based pricing pairs naturally with verified outcomes: charge only for results your CRM confirmed. See outcome-based billing in practice for the full pattern.

FAQ

Do I need the SDK? No. The SDK is a thin convenience layer over the REST API — you can route LLM calls through the proxy and post signals with plain HTTP. The SDK just makes cost capture, signal reporting, and billing setup one-liners in Python and TypeScript. How do I bill by outcome with the SDK? Report results with outcome(event_name, value_cents=...), then create an outcome meter and price. setup_billing(kind="outcome", signal_name=..., price_per_unit_cents=...) does both in one call, so each reported outcome bills your customer automatically. Mark outcomes verified to bill only on results a trusted source confirmed. Which languages are supported? Python (the macropay package) and TypeScript (@macropay/sdk) have full parity for agent instrumentation. Both expose openai_base_url / openaiBaseUrl and AgentInstrumentation. Any other language can use the REST API directly. Does the proxy add latency? The proxy forwards your request to the upstream provider and streams the response back; metering happens on the response, not in the critical path. You also get real-time usage and budget headers on every call — see LLM inference. How is the LLM cost attributed to the right agent? Through the proxy key. Mint a proxy key bound to the agent, set it as the OpenAI client’s api_key, and every call through openai_base_url() records its token cost against that agent — feeding Agentic Margin with zero extra code.