Skip to content

[P1] Add token tracking after LLM calls in WorkerAgent #99

Description

@frankbria

Overview

After implementing actual LLM calls in execute_task(), we need to track token usage for cost monitoring and metrics.

Current State

File: codeframe/agents/worker_agent.py line 98

# TODO: Add token tracking after LLM call (see docstring example)

Expected Behavior

After each LLM API call:

  • Extract input_tokens and output_tokens from response
  • Record token usage via MetricsTracker.record_token_usage()
  • Store in database with task_id, agent_id, model_name, timestamp
  • Calculate cost using MODEL_PRICING constants

Implementation Requirements

  1. Use the pattern shown in the docstring (lines 82-93)
  2. Call MetricsTracker.record_token_usage() after each API call
  3. Handle cases where usage info is missing (graceful degradation)
  4. Support multiple models (Sonnet 4.5, Opus 4, Haiku 4)

Acceptance Criteria

  • Token usage recorded after every LLM call
  • Data includes task_id, agent_id, project_id, model, tokens
  • Cost calculated correctly using MODEL_PRICING
  • Dashboard displays real-time token usage
  • Historical metrics queryable via API

Priority

P1 - Critical: Required for cost monitoring and preventing budget overruns.

Dependencies

References

  • Docstring example: worker_agent.py lines 82-93
  • MetricsTracker: codeframe/lib/metrics_tracker.py
  • User Documentation: CODEFRAME_USER_DOCUMENTATION.md lines 299-304

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions