Return normalized token usage from one-shot AI calls #15
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Goal
Return provider token accounting with every successful core one-shot AI result.
Current state
crates/bds-core/src/engine/ai.rs::run_one_shotparseschoices[0].message.contentand ignores the responseusageobject.chat_messages.token_usage_inputandchat_messages.token_usage_outputbeside the existing cache counters.specs/ai.allium,UsageReturnedWithSuccessfulResult, is normative.Required behavior
Every successful one-shot call returns its existing operation result plus a usage value with nullable:
input_tokensoutput_tokenscache_read_tokenscache_write_tokensNormalize OpenAI-compatible fields exactly as bDS2 does:
usage.prompt_tokens->input_tokensusage.completion_tokens->output_tokensusage.prompt_tokens_details.cached_tokens->cache_read_tokensusage.completion_tokens_details.cached_tokens->cache_write_tokensMissing usage or missing individual counters produces
Nonefor those counters and does not fail an otherwise valid AI result.One-shot calls do not create a chat conversation and do not persist
ChatMessagerows. Persisting normalized counters on assistant chat messages belongs to the extension chat implementation; the schema fields are present for that future work.Implementation notes
../bDS2/lib/bds/ai/openai_compatible_runtime.ex,one_shot.ex, andchat.exwhen the spec is silent.Acceptance criteria
allium check specs/*.allium,cargo fmt --all -- --check,cargo build --workspace, andcargo test --workspacepass.Implemented in
33929fd.run_one_shotnow returns(OneShotResponse, TokenUsage); one sharedparse_token_usageat the HTTP response boundary normalizesprompt_tokens->input_tokens,completion_tokens->output_tokens,prompt_tokens_details.cached_tokens->cache_read_tokens,completion_tokens_details.cached_tokens->cache_write_tokens. Missing or non-numeric counters stay null without failing the result.allium check,cargo fmt --check,cargo build --workspace,cargo test --workspaceall pass.