Multi-Agent Cost Tracking
Track costs per agent using separate Solwyn instances with distinct project API keys
import os
from openai import OpenAI
from solwyn import Solwyn
research_agent = Solwyn(
OpenAI(),
api_key=os.environ["SOLWYN_RESEARCH_API_KEY"],
)
writing_agent = Solwyn(
OpenAI(),
api_key=os.environ["SOLWYN_WRITING_API_KEY"],
)
# Each agent's costs are tracked separately in the Solwyn dashboard
research = research_agent.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Research quantum computing advances in 2025."}],
)
draft = writing_agent.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a technical writer."},
{"role": "user", "content": f"Write an article based on: {research.choices[0].message.content}"},
],
)
print(draft.choices[0].message.content)
research_agent.close()
writing_agent.close()Use separate Solwyn instances with distinct project API keys to track costs per agent, team, or workload. Each project key maps to an independent budget and usage dashboard in Solwyn Cloud — create one key per project from the Solwyn dashboard.
Two ways to attribute cost
There are two complementary mechanisms, and which you reach for depends on how much isolation the agents need:
| Mechanism | One per | Spending cap | Use when |
|---|---|---|---|
| Project keys | agent / team / workload | Its own budget, isolated from every other key | Agents need hard isolation — independent budgets, separate dashboards. |
Agent runs (solwyn.run) | unit of work | Shares the key's budget, and can carry a per-run cap (SDK v0.3.0+) | One agent (or pipeline) where you want per-run cost/latency grouping, tags, and a ceiling on any single run. |
Use separate keys when workloads must not be able to spend each other's money; use agent runs for fine-grained attribution — and per-run ceilings — within one key. They compose: open runs inside a per-agent key. Per-run caps are configured in Cloud, not in the SDK — see Per-run budget caps.
Grouping calls with agent runs
When several tasks share one project key, wrap each unit of work with solwyn.run(name) so the dashboard groups its cost and latency separately:
import os
import solwyn
from openai import OpenAI
client = solwyn.Solwyn(OpenAI(), api_key=os.environ["SOLWYN_API_KEY"])
for document in documents:
with solwyn.run("summarize-document", tags={"team": "research", "env": "prod"}):
client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": f"Summarize: {document}"}],
)
client.close()Each iteration produces a distinct run id (the dashboard aggregates by id, not name), so you can see the cost distribution across documents. The optional tags give you a second axis to slice on — team, environment, tenant. See Agent Runs for tags, nesting, async, and threading details.
Per-agent budgets
Each agent can have its own budget mode and limits:
import os
from openai import OpenAI
from solwyn import BudgetExceededError, Solwyn
# Research agent: generous budget, alert-only
research_agent = Solwyn(
OpenAI(),
api_key=os.environ["SOLWYN_RESEARCH_API_KEY"],
budget_mode="alert_only",
)
# Customer-facing agent: strict budget, hard deny
customer_agent = Solwyn(
OpenAI(),
api_key=os.environ["SOLWYN_CUSTOMER_API_KEY"],
budget_mode="hard_deny",
)
try:
response = customer_agent.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Help me with my order."}],
)
print(response.choices[0].message.content)
except BudgetExceededError:
print("Customer agent budget exhausted -- falling back to static response.")
research_agent.close()
customer_agent.close()Agent registry pattern
For applications with many agents, use a registry to manage Solwyn instances:
from openai import OpenAI
from solwyn import Solwyn
class AgentRegistry:
def __init__(self):
self._agents: dict[str, Solwyn] = {}
def get_or_create(self, agent_name: str, api_key: str) -> Solwyn:
if agent_name not in self._agents:
self._agents[agent_name] = Solwyn(
OpenAI(),
api_key=api_key,
)
return self._agents[agent_name]
def close_all(self):
for agent in self._agents.values():
agent.close()
self._agents.clear()
# Usage -- one Solwyn project key per agent
import os
registry = AgentRegistry()
research = registry.get_or_create("research", os.environ["SOLWYN_RESEARCH_API_KEY"])
writing = registry.get_or_create("writing", os.environ["SOLWYN_WRITING_API_KEY"])
review = registry.get_or_create("review", os.environ["SOLWYN_REVIEW_API_KEY"])
# Each agent call is attributed to its project
response = research.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Summarize recent AI papers."}],
)
# Clean up all agents
registry.close_all()Mixed providers
Different agents can use different LLM providers. Each is tracked independently:
import os
from anthropic import Anthropic
from openai import OpenAI
from solwyn import Solwyn
# OpenAI for fast tasks
fast_agent = Solwyn(
OpenAI(),
api_key=os.environ["SOLWYN_FAST_API_KEY"],
)
# Anthropic for complex reasoning
reasoning_agent = Solwyn(
Anthropic(),
api_key=os.environ["SOLWYN_REASONING_API_KEY"],
)
# Fast agent handles simple queries
summary = fast_agent.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Summarize this in one line: ..."}],
)
# Reasoning agent handles complex analysis
analysis = reasoning_agent.messages.create(
model="claude-sonnet-4-5",
max_tokens=2048,
messages=[{"role": "user", "content": "Analyze the implications of: ..."}],
)
fast_agent.close()
reasoning_agent.close()In the Solwyn dashboard, the fast-tasks project shows OpenAI costs and the reasoning project shows Anthropic costs -- each with its own usage graphs and budget controls.