peargent.

Cost Tracking

Track and optimize LLM API costs with automatic token counting and pricing

Peargent automatically tracks token usage and calculates costs for all LLM API calls. This helps you monitor spending, optimize prompts, and control costs in production.

How Cost Tracking Works

Cost tracking is automatic when tracing is enabled. Peargent counts tokens using tiktoken and calculates costs based on the model's pricing.

from peargent import create_agent 
from peargent.observability import enable_tracing 

# Enable tracing to start cost tracking
tracer = enable_tracing()

agent = create_agent(..., tracing=True)
result = agent.run("What is 2+2?")

# Check costs
trace = tracer.list_traces()[-1]
print(f"Total cost: ${trace.total_cost:.6f}") 

Custom Model Pricing

Add pricing for custom or new models:

tracer = enable_tracing()

# Add custom pricing (prices per million tokens)
tracer.add_custom_pricing(
    model="my-custom-model",
    prompt_price=1.50,      # $1.50 per million tokens
    completion_price=3.00   # $3.00 per million tokens
)

Cost Calculation Formula

Costs are calculated using this formula:

prompt_cost = (prompt_tokens / 1,000,000) * prompt_price
completion_cost = (completion_tokens / 1,000,000) * completion_price
total_cost = prompt_cost + completion_cost

Viewing Cost Information

You can view costs per trace or get aggregate statistics:

# Per-trace costs
for trace in tracer.list_traces():
    print(f"{trace.agent_name}: ${trace.total_cost:.6f} ({trace.total_tokens} tokens)") 

# Summary statistics
tracer.print_summary()

Best Practices

  1. Enable Tracing in Production: Always track costs in live environments.
  2. Monitor Daily: Use tracer.print_summary() to check daily spend.
  3. Set Alerts: Implement budget alerts for cost spikes.
  4. Optimize Prompts: Reduce token usage to lower costs.
  5. Use Cheaper Models: Use smaller models (e.g., gemini-2.0-flash) for simple tasks.

What's Next?

Tracing Storage Set up persistent trace storage with SQLite, PostgreSQL, or Redis for long-term cost analysis and reporting.