src.cost_tracking package#
Cost Tracking Module for Marcus.
Provides token-based cost tracking for AI usage across projects.
- class src.cost_tracking.TokenTracker[source]#
Bases:
objectTracks AI token usage per project with real-time cost monitoring.
Features: - Per-project token tracking - Real-time spend rate calculation - Cost projections based on current usage - Token usage history and analytics
- __init__(cost_per_1k_tokens=0.03)[source]#
Initialize token tracker.
- Parameters:
cost_per_1k_tokens (
float) – Cost per 1000 tokens (default $0.03 for Claude)
- async track_tokens(project_id, input_tokens, output_tokens, model='claude-3-sonnet', metadata=None)[source]#
Track token usage for a project.
- Parameters:
- Returns:
Dict with usage stats and cost info
- Return type:
- get_project_stats(project_id)[source]#
Get comprehensive stats for a project.
- Parameters:
project_id (
str) – Project identifier- Returns:
Dict containing: - total_tokens: Total tokens used - total_cost: Total cost incurred - current_spend_rate: Tokens/hour over last 5 minutes - average_spend_rate: Overall tokens/hour - projected_cost: Estimated total cost at current rate - session_duration: Time since first token
- Return type:
- class src.cost_tracking.AIUsageMiddleware[source]#
Bases:
objectMiddleware that wraps AI provider calls to track token usage.
This intercepts all calls to AI providers (OpenAI, Anthropic, etc.) and tracks token consumption per project.
- set_project_context(agent_id, project_id, task_id=None)[source]#
Set the current project context for an agent.
This should be called when an agent starts working on a project/task.
- src.cost_tracking.track_project_tokens(project_id, agent_id=None)[source]#
Context manager to track AI tokens for a specific project.
- Usage:
- with track_project_tokens(“project_123”, “agent_1”):
# All AI calls in this block will be tracked to project_123 await ai_engine.analyze(…)