cost_guard_record
Track and record token usage and costs for completed LLM calls to monitor API expenses and maintain budget control.
Instructions
Record a completed LLM call's token usage and cost.
Args: model: Model identifier used for the call. input_tokens: Actual input tokens consumed. output_tokens: Actual output tokens consumed. purpose: Optional label for this call (e.g. "summarizer", "classifier").
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| model | Yes | ||
| input_tokens | Yes | ||
| output_tokens | Yes | ||
| purpose | No |
Implementation Reference
- src/agent_safety_mcp/server.py:166-187 (handler)The cost_guard_record tool implementation, which utilizes the CostGuard instance to record token usage and return the calculated cost and total spent.
def cost_guard_record( model: str, input_tokens: int, output_tokens: int, purpose: str = "", ) -> dict: """Record a completed LLM call's token usage and cost. Args: model: Model identifier used for the call. input_tokens: Actual input tokens consumed. output_tokens: Actual output tokens consumed. purpose: Optional label for this call (e.g. "summarizer", "classifier"). """ guard = _get_guard() cost = guard.record(model, input_tokens, output_tokens, purpose=purpose) return { "recorded": True, "model": model, "cost_usd": round(cost, 6), "total_spent_usd": round(guard.status().get("spent_usd", 0), 6), }