gadget/gadget-drone
Rob Colbert 00969cf9bc feat: token economics — real API token extraction, stats propagation, and context window fuel gauge
Phase 1: OpenAI API Token Extraction
- Add stream_options: { include_usage: true } to all streaming API calls
- Capture chunk.usage from final streaming chunks and response.usage from non-streaming
- Extend OpenAiChatIterationResult with optional usage field
- Update buildStats() to accept and return real token counts from usage data
- Wire iteration.usage through chat() to both buildStats() call sites

Phase 2: Agent Loop Stats Propagation
- Rename inputTokens/outputTokens to masterInputTokens/masterOutputTokens, add masterThinkingTokens
- Accumulate response.stats.tokenCounts after each master AI call
- Delete all Math.ceil(length/4) crude approximations (master and subagent loops)
- Track startTime/durationMs and emit IWorkOrderCompleteStats with workOrderComplete
- Subagent loop uses response.stats?.tokenCounts instead of Math.ceil

Phase 3: Database Model Changes
- Add contextWindowUsage field to IChatSession, ChatSessionSchema, and frontend ChatSession
- Initialize contextWindowUsage: 0 on session creation

Phase 4: Persist Stats on Turn Completion
- drone-session: accept IWorkOrderCompleteStats, persist turn stats, walk subagent records for aggregate
- drone-session: $inc session stats and contextWindowUsage, add formatDurationLabel() helper
- code-session: accept and forward stats, update in-memory session stats
- message-queue: Redis replay handles 4th stats arg
- Update WorkOrderCompleteMessage type in @gadget/api to accept stats parameter

Phase 5: UI — Context Window Fuel Gauge
- Add contextWindowUsage prop to SessionPanel
- Add fuel gauge bar with E→F labels, green/yellow/red zones, token count display
- Visible for ALL provider types (not gated by apiType)

Phase 6: Frontend Streaming State
- Add IWorkOrderCompleteStats interface to frontend api.ts
- handleWorkOrderComplete accepts stats, updates turn stats and session contextWindowUsage
- Pass contextWindowUsage prop to SessionPanel
2026-05-18 14:36:36 -04:00
..
.vscode created by merging gadget-code and gadget-drone 2026-04-28 09:20:37 -04:00
data/prompts/subagent subagents (written by an agent, for an agent) 2026-05-11 11:22:59 -04:00
docs Phase 2: SubProcess observability in DroneManager and DroneInspector 2026-05-14 13:52:59 -04:00
src feat: token economics — real API token extraction, stats propagation, and context window fuel gauge 2026-05-18 14:36:36 -04:00
types created by merging gadget-code and gadget-drone 2026-04-28 09:20:37 -04:00
.gitignore stop monitoring my .gadget directory, dammit! 2026-04-30 16:54:44 -04:00
AGENTS.md finishing touches on move to GadgetId from ObjectId 2026-05-01 15:07:37 -04:00
gadget-drone.code-workspace created by merging gadget-code and gadget-drone 2026-04-28 09:20:37 -04:00
LICENSE created by merging gadget-code and gadget-drone 2026-04-28 09:20:37 -04:00
package.json AI toolbox refactor/extraction complete (checkpoint) 2026-05-16 17:06:08 -04:00
pnpm-lock.yaml created by merging gadget-code and gadget-drone 2026-04-28 09:20:37 -04:00
pnpm-workspace.yaml created by merging gadget-code and gadget-drone 2026-04-28 09:20:37 -04:00
tsconfig.json created by merging gadget-code and gadget-drone 2026-04-28 09:20:37 -04:00
vitest.config.ts Phase 1: SubProcess service and agent tool 2026-05-14 12:59:05 -04:00