方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

Exercise

Track token usage and cost per model across async calls

Track token usage and cost per model across async calls

Cost visibility is the first step to cost control. This exercise builds a thread-safe cost tracker that can be used as a decorator around any async LLM call.

What you are building

Implement a CostTracker class and a track decorator:

CostTracker:

  • record(model: str, input_tokens: int, output_tokens: int) -> None — Record a single call (thread-safe)
  • total_cost_usd(model: str | None = None) -> float — Total cost across all models, or for a specific model
  • summary() -> dict — Per-model breakdown: {model: {"calls": int, "input_tokens": int, "output_tokens": int, "cost_usd": float}}

Pricing (per million tokens):

"claude-haiku-4-5":  input=$0.80, output=$4.00
"claude-sonnet-4-5": input=$3.00, output=$15.00
"gpt-4o-mini":       input=$0.15, output=$0.60
"gpt-4o":            input=$2.50, output=$10.00

For unknown models, use $3.00/$15.00 as the default rate.

track decorator:

Implement track(tracker: CostTracker, model: str) as a decorator factory. It wraps an async function that returns a dict with input_tokens and output_tokens keys. After each successful call, it records the usage in the tracker.

Example

tracker = CostTracker()

@track(tracker, model="gpt-4o-mini")
async def call_openai(prompt: str) -> dict:
    return {"text": "response", "input_tokens": 100, "output_tokens": 50}

asyncio.run(call_openai("hello"))
print(tracker.summary())
# {"gpt-4o-mini": {"calls": 1, "input_tokens": 100, "output_tokens": 50, "cost_usd": 0.0000450}}

Api Async / medium / Step 21 of 23

Practice stage

Async and provider control

Hint

Make waiting behavior explicit. Timeouts, retries, and concurrency limits matter more than squeezing everything into one helper.

Success criteria
  • - Uses async boundaries coherently
  • - Makes timeout and retry decisions legible
  • - Would be maintainable under provider instability
Review checklist
  • - Is timeout behavior explicit?
  • - Is retryable failure separate from terminal failure?
  • - Would logs reveal what actually timed out?

Practice

Generate a variation

Generate a new exercise variation to deepen understanding or practice a related concept.

Attempt history

Recent submissions

Before you submit, decide what a strong answer should make obvious to the reviewer.

No attempts yet.