Add per-completion observability records and cache-aware cost accounting (#458) - #464
Open
wschenk wants to merge 1 commit into
Open
Add per-completion observability records and cache-aware cost accounting (#458)#464wschenk wants to merge 1 commit into
wschenk wants to merge 1 commit into
Conversation
…ing (#458) - New packages/core/src/observability: CompletionRecord, CompletionSink, JsonlCompletionSink (<project>/.umwelten/completions/YYYY-MM-DD.jsonl), MemoryCompletionSink, NullCompletionSink; UMWELTEN_TRACE=0 disables, UMWELTEN_COMPLETIONS_DIR overrides the directory. - BaseModelRunner emits one record per generate/stream call from makeResult (completed), the aborted-stream path, and error paths; ModelRunnerConfig.sink overrides the process default. - TokenUsage/CostBreakdown gain cacheRead/cacheWrite/reasoning fields; normalizeTokenUsage maps AI SDK v5/v7, OpenAI and raw Anthropic shapes; calculateCost prices cache tokens at their own rate; OpenRouter pricing reads input_cache_read/input_cache_write. - Interaction.messageUsage tags each assistant message; transcript-write persists Claude-shaped `usage` on assistant entries so `sessions show` reports cache reads for native umwelten transcripts. - ResponseMetadata.cost is now optional (free models / no usage). - Design report: reports/2026-09-02-observability-and-cheapest-model-mvp.md Amp-Thread-ID: https://ampcode.com/threads/T-01a063b8-8fec-76fd-8d2a-6a5afd6fc60f Co-authored-by: Will Schenk <wschenk@thefocus.ai>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #458 (step 1–2 of epic #457). Design:
reports/2026-09-02-observability-and-cheapest-model-mvp.md.What
packages/core/src/observability/:CompletionRecord,CompletionSink,JsonlCompletionSink(<project>/.umwelten/completions/YYYY-MM-DD.jsonl),MemoryCompletionSink,NullCompletionSink.UMWELTEN_TRACE=0disables;UMWELTEN_COMPLETIONS_DIRoverrides;new BaseModelRunner({ sink })overrides in-process.BaseModelRunneremits one record per call frommakeResult(all four ops), the aborted-stream path, and error paths.TokenUsage/CostBreakdowngain cacheRead/cacheWrite/reasoning;normalizeTokenUsagemaps AI SDK v5/v7, OpenAI and raw Anthropic shapes;calculateCostis cache-aware with input-rate fallback; OpenRouter readsinput_cache_read/_writepricing.Interaction.messageUsage+writeSessionTranscript(..., usageFor)persist Claude-shapedusageon assistant entries;sessions showrenders cache rows when reads or writes exist.ResponseMetadata.costis now optional (already undefined at runtime for free models; all callers guarded it).Verification
pnpm test:run: 3074 passed; onlyagent-runner-tools.test.tsfails (git signing key in sandbox temp repo — pre-existing, unrelated).pnpm typecheckclean; eslint 0 errors on touched files.MockLanguageModelV3through the realgenerateTextpath: JSONL record hastokens.cacheRead: 800,cost.total = 0.00159(200×$3 + 800×$0.30 + 50×$15 per M), andsessions show --session-dirprintsCache reads 800from the written transcript.