SDKs
Python SDK
The Agentrics Python SDK wraps your OpenAI and Anthropic clients to automatically track every API call. It requires Python 3.8+ and has no third-party dependencies.
Installation
pip install agent-cost-optimizerAgentrics(api_key)
Create a single Agentrics instance at startup and use it to wrap all your AI clients.
from agent_cost_optimizer import Agentrics
agentrics = Agentrics(api_key="agntcs_sk_...")| Parameter | Type | Description |
|---|---|---|
api_keyrequired | str | Your Agentrics API key. Generate one from Settings → API Keys. |
Base URL override: Set the
AGENTRICS_BASE_URL environment variable to point at a different server (e.g. http://localhost:3000 during local development). By default the SDK posts to https://www.agentrics.io.wrap_anthropic(client, agent_id, agent_name?)
Wraps an anthropic.Anthropic() instance. The returned client has the same interface — use it exactly as you would the original.
from anthropic import Anthropic
from agent_cost_optimizer import Agentrics
agentrics = Agentrics(api_key="agntcs_sk_...")
client = agentrics.wrap_anthropic(
Anthropic(),
agent_id="support-classifier",
agent_name="Support Classifier", # optional, defaults to agent_id
)
# Use identically to the original client
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.content)| Parameter | Type | Description |
|---|---|---|
clientrequired | Anthropic | An anthropic.Anthropic() instance. |
agent_idrequired | str | Stable identifier for this agent, e.g. "summarizer". Used to group executions in the dashboard. |
agent_name | str | Human-readable display name. Defaults to agent_id. |
wrap_openai(client, agent_id, agent_name?)
Wraps an openai.OpenAI() instance. Intercepts all chat.completions.create() calls.
from openai import OpenAI
from agent_cost_optimizer import Agentrics
agentrics = Agentrics(api_key="agntcs_sk_...")
client = agentrics.wrap_openai(
OpenAI(),
agent_id="research-agent",
)
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Summarize this..."}],
)
print(response.choices[0].message.content)| Parameter | Type | Description |
|---|---|---|
clientrequired | OpenAI | An openai.OpenAI() instance. |
agent_idrequired | str | Stable identifier for this agent, e.g. "researcher". |
agent_name | str | Human-readable display name. Defaults to agent_id. |
Error handling
Tracking is fire-and-forget — if the Agentrics API is unreachable, the error is silently swallowed and your AI call is unaffected. If the underlying AI API raises an exception, the SDK records the failure (with success=False) before re-raising it.