feat(token-tracking): record AI token consumption per task (CUST-WP-0029)
Introduces end-to-end token consumption tracking so agent work is
visible as a cost/effort metric alongside tasks and workplans.
- Migration o2j3k4l5m6n7: token_events table with FK indexes on
task_id, workstream_id, repo_id, created_at
- ORM model, Pydantic schemas (TokenEventCreate, TokenEventRead with
computed tokens_total, TokenSummary)
- Router: POST /token-events/, GET /token-events/ (7 filters),
GET /token-events/summary/ (task|workstream|repo|commit|release scope)
- MCP tools: record_token_event, get_token_summary (formatted table)
- update_task_status enriched with optional tokens_in/tokens_out
passthrough — one call creates status update + token event
- Dashboard token-cost.md page: by-repo bar, by-workplan table,
by-model bar, top-10 tasks by tokens
- ralph-workplan skill updated with token reporting guidance and
per-task heuristics for estimating counts
- Tests: test_token_events.py + test_token_passthrough.py (182 pass)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-29 17:46:46 +02:00
|
|
|
import uuid
|
|
|
|
|
from datetime import datetime
|
2026-05-23 13:59:05 +02:00
|
|
|
from typing import Any
|
feat(token-tracking): record AI token consumption per task (CUST-WP-0029)
Introduces end-to-end token consumption tracking so agent work is
visible as a cost/effort metric alongside tasks and workplans.
- Migration o2j3k4l5m6n7: token_events table with FK indexes on
task_id, workstream_id, repo_id, created_at
- ORM model, Pydantic schemas (TokenEventCreate, TokenEventRead with
computed tokens_total, TokenSummary)
- Router: POST /token-events/, GET /token-events/ (7 filters),
GET /token-events/summary/ (task|workstream|repo|commit|release scope)
- MCP tools: record_token_event, get_token_summary (formatted table)
- update_task_status enriched with optional tokens_in/tokens_out
passthrough — one call creates status update + token event
- Dashboard token-cost.md page: by-repo bar, by-workplan table,
by-model bar, top-10 tasks by tokens
- ralph-workplan skill updated with token reporting guidance and
per-task heuristics for estimating counts
- Tests: test_token_events.py + test_token_passthrough.py (182 pass)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-29 17:46:46 +02:00
|
|
|
|
2026-06-22 13:52:13 +02:00
|
|
|
from pydantic import AliasChoices, BaseModel, ConfigDict, Field, computed_field
|
|
|
|
|
|
|
|
|
|
from api.schemas.compat import OptionalWorkplanIdCompatMixin
|
feat(token-tracking): record AI token consumption per task (CUST-WP-0029)
Introduces end-to-end token consumption tracking so agent work is
visible as a cost/effort metric alongside tasks and workplans.
- Migration o2j3k4l5m6n7: token_events table with FK indexes on
task_id, workstream_id, repo_id, created_at
- ORM model, Pydantic schemas (TokenEventCreate, TokenEventRead with
computed tokens_total, TokenSummary)
- Router: POST /token-events/, GET /token-events/ (7 filters),
GET /token-events/summary/ (task|workstream|repo|commit|release scope)
- MCP tools: record_token_event, get_token_summary (formatted table)
- update_task_status enriched with optional tokens_in/tokens_out
passthrough — one call creates status update + token event
- Dashboard token-cost.md page: by-repo bar, by-workplan table,
by-model bar, top-10 tasks by tokens
- ralph-workplan skill updated with token reporting guidance and
per-task heuristics for estimating counts
- Tests: test_token_events.py + test_token_passthrough.py (182 pass)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-29 17:46:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TokenEventCreate(BaseModel):
|
|
|
|
|
tokens_in: int
|
|
|
|
|
tokens_out: int
|
|
|
|
|
task_id: uuid.UUID | None = None
|
2026-06-22 13:52:13 +02:00
|
|
|
workplan_id: uuid.UUID | None = Field(
|
|
|
|
|
default=None,
|
|
|
|
|
validation_alias=AliasChoices("workplan_id", "workstream_id"),
|
|
|
|
|
)
|
feat(token-tracking): record AI token consumption per task (CUST-WP-0029)
Introduces end-to-end token consumption tracking so agent work is
visible as a cost/effort metric alongside tasks and workplans.
- Migration o2j3k4l5m6n7: token_events table with FK indexes on
task_id, workstream_id, repo_id, created_at
- ORM model, Pydantic schemas (TokenEventCreate, TokenEventRead with
computed tokens_total, TokenSummary)
- Router: POST /token-events/, GET /token-events/ (7 filters),
GET /token-events/summary/ (task|workstream|repo|commit|release scope)
- MCP tools: record_token_event, get_token_summary (formatted table)
- update_task_status enriched with optional tokens_in/tokens_out
passthrough — one call creates status update + token event
- Dashboard token-cost.md page: by-repo bar, by-workplan table,
by-model bar, top-10 tasks by tokens
- ralph-workplan skill updated with token reporting guidance and
per-task heuristics for estimating counts
- Tests: test_token_events.py + test_token_passthrough.py (182 pass)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-29 17:46:46 +02:00
|
|
|
repo_id: uuid.UUID | None = None
|
|
|
|
|
session_id: str | None = None
|
|
|
|
|
model: str | None = None
|
|
|
|
|
agent: str | None = None
|
|
|
|
|
ref_type: str | None = None
|
|
|
|
|
ref_id: str | None = None
|
|
|
|
|
note: str | None = None
|
2026-05-23 13:59:05 +02:00
|
|
|
created_at: datetime | None = None
|
|
|
|
|
measurement_kind: str | None = None
|
|
|
|
|
source_provider: str | None = None
|
|
|
|
|
source_id: str | None = None
|
|
|
|
|
source_path: str | None = None
|
|
|
|
|
source_created_at: datetime | None = None
|
|
|
|
|
parser_version: str | None = None
|
|
|
|
|
confidence: float | None = None
|
|
|
|
|
cached_input_tokens: int | None = None
|
|
|
|
|
reasoning_output_tokens: int | None = None
|
|
|
|
|
raw_total_tokens: int | None = None
|
|
|
|
|
cost_estimated_usd: float | None = None
|
|
|
|
|
raw_metadata: dict[str, Any] | None = None
|
feat(token-tracking): record AI token consumption per task (CUST-WP-0029)
Introduces end-to-end token consumption tracking so agent work is
visible as a cost/effort metric alongside tasks and workplans.
- Migration o2j3k4l5m6n7: token_events table with FK indexes on
task_id, workstream_id, repo_id, created_at
- ORM model, Pydantic schemas (TokenEventCreate, TokenEventRead with
computed tokens_total, TokenSummary)
- Router: POST /token-events/, GET /token-events/ (7 filters),
GET /token-events/summary/ (task|workstream|repo|commit|release scope)
- MCP tools: record_token_event, get_token_summary (formatted table)
- update_task_status enriched with optional tokens_in/tokens_out
passthrough — one call creates status update + token event
- Dashboard token-cost.md page: by-repo bar, by-workplan table,
by-model bar, top-10 tasks by tokens
- ralph-workplan skill updated with token reporting guidance and
per-task heuristics for estimating counts
- Tests: test_token_events.py + test_token_passthrough.py (182 pass)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-29 17:46:46 +02:00
|
|
|
|
|
|
|
|
|
2026-06-22 13:52:13 +02:00
|
|
|
class TokenEventRead(OptionalWorkplanIdCompatMixin, BaseModel):
|
feat(token-tracking): record AI token consumption per task (CUST-WP-0029)
Introduces end-to-end token consumption tracking so agent work is
visible as a cost/effort metric alongside tasks and workplans.
- Migration o2j3k4l5m6n7: token_events table with FK indexes on
task_id, workstream_id, repo_id, created_at
- ORM model, Pydantic schemas (TokenEventCreate, TokenEventRead with
computed tokens_total, TokenSummary)
- Router: POST /token-events/, GET /token-events/ (7 filters),
GET /token-events/summary/ (task|workstream|repo|commit|release scope)
- MCP tools: record_token_event, get_token_summary (formatted table)
- update_task_status enriched with optional tokens_in/tokens_out
passthrough — one call creates status update + token event
- Dashboard token-cost.md page: by-repo bar, by-workplan table,
by-model bar, top-10 tasks by tokens
- ralph-workplan skill updated with token reporting guidance and
per-task heuristics for estimating counts
- Tests: test_token_events.py + test_token_passthrough.py (182 pass)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-29 17:46:46 +02:00
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
|
|
|
|
|
|
id: uuid.UUID
|
|
|
|
|
tokens_in: int
|
|
|
|
|
tokens_out: int
|
|
|
|
|
task_id: uuid.UUID | None = None
|
|
|
|
|
repo_id: uuid.UUID | None = None
|
|
|
|
|
session_id: str | None = None
|
|
|
|
|
model: str | None = None
|
|
|
|
|
agent: str | None = None
|
|
|
|
|
ref_type: str | None = None
|
|
|
|
|
ref_id: str | None = None
|
|
|
|
|
note: str | None = None
|
2026-05-23 13:59:05 +02:00
|
|
|
measurement_kind: str
|
|
|
|
|
source_provider: str
|
|
|
|
|
source_id: str | None = None
|
|
|
|
|
source_path: str | None = None
|
|
|
|
|
source_created_at: datetime | None = None
|
|
|
|
|
ingested_at: datetime
|
|
|
|
|
parser_version: str | None = None
|
|
|
|
|
confidence: float
|
|
|
|
|
cached_input_tokens: int
|
|
|
|
|
reasoning_output_tokens: int
|
|
|
|
|
raw_total_tokens: int | None = None
|
|
|
|
|
cost_estimated_usd: float | None = None
|
|
|
|
|
raw_metadata: dict[str, Any] = Field(default_factory=dict)
|
feat(token-tracking): record AI token consumption per task (CUST-WP-0029)
Introduces end-to-end token consumption tracking so agent work is
visible as a cost/effort metric alongside tasks and workplans.
- Migration o2j3k4l5m6n7: token_events table with FK indexes on
task_id, workstream_id, repo_id, created_at
- ORM model, Pydantic schemas (TokenEventCreate, TokenEventRead with
computed tokens_total, TokenSummary)
- Router: POST /token-events/, GET /token-events/ (7 filters),
GET /token-events/summary/ (task|workstream|repo|commit|release scope)
- MCP tools: record_token_event, get_token_summary (formatted table)
- update_task_status enriched with optional tokens_in/tokens_out
passthrough — one call creates status update + token event
- Dashboard token-cost.md page: by-repo bar, by-workplan table,
by-model bar, top-10 tasks by tokens
- ralph-workplan skill updated with token reporting guidance and
per-task heuristics for estimating counts
- Tests: test_token_events.py + test_token_passthrough.py (182 pass)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-29 17:46:46 +02:00
|
|
|
created_at: datetime
|
|
|
|
|
|
|
|
|
|
@computed_field
|
|
|
|
|
@property
|
|
|
|
|
def tokens_total(self) -> int:
|
|
|
|
|
return self.tokens_in + self.tokens_out
|
|
|
|
|
|
2026-05-23 13:59:05 +02:00
|
|
|
@computed_field
|
|
|
|
|
@property
|
|
|
|
|
def token_evidence_total(self) -> int:
|
|
|
|
|
return (self.raw_total_tokens or self.tokens_in + self.tokens_out)
|
|
|
|
|
|
feat(token-tracking): record AI token consumption per task (CUST-WP-0029)
Introduces end-to-end token consumption tracking so agent work is
visible as a cost/effort metric alongside tasks and workplans.
- Migration o2j3k4l5m6n7: token_events table with FK indexes on
task_id, workstream_id, repo_id, created_at
- ORM model, Pydantic schemas (TokenEventCreate, TokenEventRead with
computed tokens_total, TokenSummary)
- Router: POST /token-events/, GET /token-events/ (7 filters),
GET /token-events/summary/ (task|workstream|repo|commit|release scope)
- MCP tools: record_token_event, get_token_summary (formatted table)
- update_task_status enriched with optional tokens_in/tokens_out
passthrough — one call creates status update + token event
- Dashboard token-cost.md page: by-repo bar, by-workplan table,
by-model bar, top-10 tasks by tokens
- ralph-workplan skill updated with token reporting guidance and
per-task heuristics for estimating counts
- Tests: test_token_events.py + test_token_passthrough.py (182 pass)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-29 17:46:46 +02:00
|
|
|
|
|
|
|
|
class TokenSummary(BaseModel):
|
|
|
|
|
scope: str
|
|
|
|
|
scope_id: str
|
|
|
|
|
tokens_in: int
|
|
|
|
|
tokens_out: int
|
|
|
|
|
tokens_total: int
|
|
|
|
|
event_count: int
|
|
|
|
|
by_model: dict[str, int]
|
|
|
|
|
by_agent: dict[str, int]
|
2026-05-23 13:59:05 +02:00
|
|
|
by_measurement_kind: dict[str, int] = Field(default_factory=dict)
|
|
|
|
|
by_source_provider: dict[str, int] = Field(default_factory=dict)
|
2026-03-29 19:05:23 +02:00
|
|
|
|
|
|
|
|
|
2026-04-01 22:38:45 +02:00
|
|
|
class TokenEventPatch(BaseModel):
|
|
|
|
|
tokens_in: int | None = None
|
|
|
|
|
tokens_out: int | None = None
|
2026-05-23 13:59:05 +02:00
|
|
|
task_id: uuid.UUID | None = None
|
2026-06-22 13:52:13 +02:00
|
|
|
workplan_id: uuid.UUID | None = Field(
|
|
|
|
|
default=None,
|
|
|
|
|
validation_alias=AliasChoices("workplan_id", "workstream_id"),
|
|
|
|
|
)
|
2026-05-23 13:59:05 +02:00
|
|
|
repo_id: uuid.UUID | None = None
|
|
|
|
|
session_id: str | None = None
|
2026-04-01 22:38:45 +02:00
|
|
|
note: str | None = None
|
|
|
|
|
model: str | None = None
|
|
|
|
|
agent: str | None = None
|
2026-05-23 13:59:05 +02:00
|
|
|
ref_type: str | None = None
|
|
|
|
|
ref_id: str | None = None
|
|
|
|
|
created_at: datetime | None = None
|
|
|
|
|
measurement_kind: str | None = None
|
|
|
|
|
source_provider: str | None = None
|
|
|
|
|
source_id: str | None = None
|
|
|
|
|
source_path: str | None = None
|
|
|
|
|
source_created_at: datetime | None = None
|
|
|
|
|
ingested_at: datetime | None = None
|
|
|
|
|
parser_version: str | None = None
|
|
|
|
|
confidence: float | None = None
|
|
|
|
|
cached_input_tokens: int | None = None
|
|
|
|
|
reasoning_output_tokens: int | None = None
|
|
|
|
|
raw_total_tokens: int | None = None
|
|
|
|
|
cost_estimated_usd: float | None = None
|
|
|
|
|
raw_metadata: dict[str, Any] | None = None
|
2026-04-01 22:38:45 +02:00
|
|
|
|
|
|
|
|
|
2026-03-29 19:05:23 +02:00
|
|
|
class RepoTokenSummary(BaseModel):
|
|
|
|
|
repo_id: uuid.UUID
|
|
|
|
|
repo_slug: str
|
|
|
|
|
tokens_in: int
|
|
|
|
|
tokens_out: int
|
|
|
|
|
tokens_total: int
|
|
|
|
|
event_count: int
|
|
|
|
|
by_model: dict[str, int]
|
|
|
|
|
by_note: dict[str, int]
|
2026-05-23 13:59:05 +02:00
|
|
|
by_measurement_kind: dict[str, int] = Field(default_factory=dict)
|
|
|
|
|
by_source_provider: dict[str, int] = Field(default_factory=dict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TokenAggregateRow(BaseModel):
|
|
|
|
|
scope_id: str
|
|
|
|
|
label: str | None = None
|
|
|
|
|
tokens_in: int
|
|
|
|
|
tokens_out: int
|
|
|
|
|
tokens_total: int
|
|
|
|
|
event_count: int
|
|
|
|
|
by_measurement_kind: dict[str, int] = Field(default_factory=dict)
|
|
|
|
|
by_source_provider: dict[str, int] = Field(default_factory=dict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TokenAggregateSummary(BaseModel):
|
|
|
|
|
tokens_in: int
|
|
|
|
|
tokens_out: int
|
|
|
|
|
tokens_total: int
|
|
|
|
|
event_count: int
|
|
|
|
|
first_event_at: datetime | None = None
|
|
|
|
|
last_event_at: datetime | None = None
|
|
|
|
|
last_ingested_at: datetime | None = None
|
|
|
|
|
by_repo: list[TokenAggregateRow] = Field(default_factory=list)
|
|
|
|
|
by_workstream: list[TokenAggregateRow] = Field(default_factory=list)
|
|
|
|
|
by_task: list[TokenAggregateRow] = Field(default_factory=list)
|
|
|
|
|
by_model: list[TokenAggregateRow] = Field(default_factory=list)
|
|
|
|
|
by_measurement_kind: dict[str, int] = Field(default_factory=dict)
|
|
|
|
|
by_source_provider: dict[str, int] = Field(default_factory=dict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TokenQualitySummary(BaseModel):
|
|
|
|
|
event_count: int
|
|
|
|
|
measured_event_count: int
|
|
|
|
|
estimated_event_count: int
|
|
|
|
|
allocated_event_count: int
|
|
|
|
|
superseded_event_count: int
|
|
|
|
|
fallback_event_count: int
|
|
|
|
|
unattributed_measured_event_count: int
|
|
|
|
|
missing_provenance_event_count: int
|
|
|
|
|
duplicate_source_count: int
|
|
|
|
|
last_codex_ingested_at: datetime | None = None
|
|
|
|
|
last_claude_ingested_at: datetime | None = None
|
|
|
|
|
last_reconciliation_at: datetime | None = None
|
|
|
|
|
by_measurement_kind: dict[str, int] = Field(default_factory=dict)
|
|
|
|
|
by_source_provider: dict[str, int] = Field(default_factory=dict)
|