---
title: "Connection: Every AI Call Needs Cost Tracker"
description: "Why ai-cost-tracker must be added at project start, not retrofitted — and how preflight/record connects to every AI provider"
connects:
  - "tech-patterns/python-ai-agents"
  - "tech-patterns/cost-tracker-integration"
  - "concepts/preflight-record-pattern"
created: 2026-04-27
updated: 2026-04-27
---

# Connection: Every AI Call Needs Cost Tracker

## The Connection

AI provider integrations (Gemini, Claude, OpenAI, ElevenLabs) and the Oliver cost tracker are not independent systems — the tracker's preflight/record pattern wraps every AI call. Projects built without cost tracking have no budget enforcement, and retroactively adding it means touching every AI call site.

## Key Insight

**Adding cost-tracker at project start costs 30 minutes. Retrofitting costs 2 days.** The preflight() call happens before the AI call, record() happens after. The tracker maintains per-project, per-team budgets and hard-blocks calls when exceeded. Without it, a runaway prompt loop or buggy retry logic can exhaust a monthly budget in minutes.

## The Pattern

```python
# Every AI call site looks like this
async def call_ai(prompt: str, user_id: str):
    # 1. Estimate and gate
    preflight_resp = await cost_tracker.post("/preflight", json={
        "source_app": "my-project",
        "model": "gemini-1.5-pro",
        "estimated_input_tokens": len(prompt) // 4,
        "estimated_output_tokens": 500,
        "user_id": user_id
    })
    if not preflight_resp.json()["allow"]:
        raise BudgetExceeded("Monthly budget reached")
    
    # 2. Make the actual AI call
    response = await gemini.generate(prompt)
    
    # 3. Record actual usage
    await cost_tracker.post("/usage/record", json={
        "source_app": "my-project",
        "model": "gemini-1.5-pro",
        "input_tokens": response.usage.input_tokens,
        "output_tokens": response.usage.output_tokens
    })
    return response
```

## Per-Provider Gotchas

| Provider | Token field | Notes |
|----------|------------|-------|
| Gemini | `response.usage_metadata.prompt_token_count` | Available after call |
| OpenAI | `response.usage.prompt_tokens` | `max_completion_tokens` not `max_tokens` for o1/o3 |
| Claude | `response.usage.input_tokens` | Via Anthropic SDK |
| ElevenLabs | `len(text)` characters | No token concept — billed by chars |
| GCP TTS | `len(text)` characters | Same as ElevenLabs |

See [[wiki/tech-patterns/cost-tracker-providers|cost-tracker-providers]] for full details.

## Why Retrofitting Is Hard

1. Every AI call site must be wrapped — in multi-agent systems, that's 4–10 places
2. The async httpx client needs to be shared (not a new client per call)
3. Budget workspace/team/project hierarchy must be set up in the admin UI first
4. Historical data is lost — you can't see what projects cost before integration

## Projects with Cost Tracker

ai-cost-tracker itself, Video Accessibility Platform, Enterprise Nexus (in progress).

## Projects that Need It Added

Any project making AI API calls without cost tracking: Mod Comms (Gemini), PIMCO Charts (Claude), Sandbox NotebookLM, Oliver AI Bot 2.0, Semblance.

## Related

- [[wiki/tech-patterns/cost-tracker-integration|cost-tracker-integration]] — 9-step integration guide
- [[wiki/tech-patterns/cost-tracker-providers|cost-tracker-providers]] — per-provider billing units
- [[wiki/concepts/preflight-record-pattern|preflight-record-pattern]] — the 3-step pattern
- [[wiki/tech-patterns/python-ai-agents|python-ai-agents]] — AI agent patterns
