kodeagent.usage_tracker#


Track and report LLM usage metrics (tokens and cost) across components.

UsageTracker

Track cumulative LLM usage across components.

Track and report LLM usage metrics (tokens and cost) across components.

class kodeagent.usage_tracker.UsageTracker[source]#

Bases: object

Track cumulative LLM usage across components.

Note: LLM usage is tracked only when calls are made via call_llm method. Any other LLM calls will not be tracked. E.g., if tools invoke LLMs directly, those calls will not be tracked.

Initialize the usage tracker.

format_report(include_breakdown: bool = True) str[source]#

Generate a formatted usage report.

Parameters:

include_breakdown – Whether to include per-component breakdown.

Returns:

Formatted string with usage statistics.

get_total_usage() ComponentUsage[source]#

Get aggregated usage across all components.

Returns:

ComponentUsage with totals across all components.

get_usage_by_component() dict[str, ComponentUsage][source]#

Get usage breakdown by component.

Returns:

Dictionary mapping component names to their usage.

async record_usage(component: str, metrics: UsageMetrics) None[source]#

Record usage from a single LLM call. Uses lock to ensure coroutine safety.

Parameters:
  • component – Name of the component making the call.

  • metrics – Usage metrics from the LLM call.

reset() None[source]#

Reset all usage tracking.