Repository navigation
feat(agent): preserve provider usage provenance #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,10 @@ export interface InvokeResult { | |
| reason?: string; | ||
| promptTokens: number | null; | ||
| completionTokens: number | null; | ||
| totalTokens: number | null; | ||
| reasoningTokens: number | null; | ||
| costUsd: number | null; | ||
| costSource: 'provider' | 'provider_upstream' | 'estimated' | null; | ||
| durationMs: number | null; | ||
| finishReason?: string; | ||
| } | ||
|
|
@@ -64,6 +68,10 @@ function notFound(reason: string): InvokeResult { | |
| reason, | ||
| promptTokens: null, | ||
| completionTokens: null, | ||
| totalTokens: null, | ||
| reasoningTokens: null, | ||
| costUsd: null, | ||
| costSource: null, | ||
| durationMs: null | ||
| }; | ||
| } | ||
|
|
@@ -79,6 +87,10 @@ function emptyResult(agent: Agent, character: Character | NPC | null, status: Ag | |
| reason, | ||
| promptTokens: null, | ||
| completionTokens: null, | ||
| totalTokens: null, | ||
| reasoningTokens: null, | ||
| costUsd: null, | ||
| costSource: null, | ||
| durationMs: null | ||
| }; | ||
| } | ||
|
|
@@ -230,6 +242,8 @@ export async function invokeAgent(input: InvokeInput, deps: AgentRuntimeDeps): P | |
| // budget cannot be bypassed by an incomplete response envelope. | ||
| const promptTokens = result.promptTokens ?? estimateTokens(composed.messages); | ||
| const completionTokens = result.completionTokens ?? estimateTokens(result.text); | ||
| const totalTokens = result.totalTokens ?? promptTokens + completionTokens; | ||
| const reasoningTokens = result.reasoningTokens ?? 0; | ||
| const tokensSpent = promptTokens + completionTokens; | ||
| if (tokensSpent > 0) { | ||
| deps.agentRepo.incrementTokensUsed(agent.id, tokensSpent); | ||
|
|
@@ -253,6 +267,10 @@ export async function invokeAgent(input: InvokeInput, deps: AgentRuntimeDeps): P | |
| rawResponse: result.raw, | ||
| promptTokens, | ||
| completionTokens, | ||
| totalTokens, | ||
| reasoningTokens, | ||
| costUsd: result.costUsd ?? null, | ||
| costSource: result.costSource ?? 'estimated', | ||
|
Comment on lines
+272
to
+273
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For every OpenAI call, and any OpenRouter response without cost metadata, Useful? React with 👍 / 👎. |
||
| durationMs: result.durationMs, | ||
| status: callStatus, | ||
| reasoningEffort: competency?.reasoningEffort ?? null, | ||
|
|
@@ -275,8 +293,12 @@ export async function invokeAgent(input: InvokeInput, deps: AgentRuntimeDeps): P | |
| response: '', | ||
| status: 'budget_exhausted', | ||
| reason: budgetMessage, | ||
| promptTokens: result.promptTokens ?? null, | ||
| completionTokens: result.completionTokens ?? null, | ||
| promptTokens, | ||
| completionTokens, | ||
| totalTokens, | ||
| reasoningTokens, | ||
| costUsd: result.costUsd ?? null, | ||
| costSource: result.costSource ?? 'estimated', | ||
| durationMs: result.durationMs, | ||
| finishReason: result.finishReason | ||
| }; | ||
|
|
@@ -308,6 +330,10 @@ export async function invokeAgent(input: InvokeInput, deps: AgentRuntimeDeps): P | |
| round: input.round ?? null, | ||
| promptTokens: result.promptTokens ?? null, | ||
| completionTokens: result.completionTokens ?? null, | ||
| totalTokens, | ||
| reasoningTokens, | ||
| costUsd: result.costUsd ?? null, | ||
| costSource: result.costSource ?? 'estimated', | ||
| durationMs: result.durationMs, | ||
| status: 'ok' | ||
| } | ||
|
|
@@ -323,8 +349,12 @@ export async function invokeAgent(input: InvokeInput, deps: AgentRuntimeDeps): P | |
| characterName: character?.name ?? null, | ||
| response: result.text, | ||
| status: 'ok', | ||
| promptTokens: result.promptTokens ?? null, | ||
| completionTokens: result.completionTokens ?? null, | ||
| promptTokens, | ||
| completionTokens, | ||
| totalTokens, | ||
| reasoningTokens, | ||
|
Comment on lines
+352
to
+355
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a compatible gateway omits usage metadata, these fields now expose locally estimated prompt/completion counts, a synthetic total, and zero reasoning tokens as though they were provider-reported; the event payload simultaneously leaves prompt/completion null. This makes downstream accounting unable to distinguish exact usage from guesses, particularly for reasoning models whose unreported reasoning usage is not necessarily zero. Preserve the optional provider values as null in the public result and use separate estimates only for internal budget enforcement. Useful? React with 👍 / 👎. |
||
| costUsd: result.costUsd ?? null, | ||
| costSource: result.costSource ?? 'estimated', | ||
| durationMs: result.durationMs, | ||
| finishReason: result.finishReason | ||
| }; | ||
|
|
@@ -368,6 +398,10 @@ export async function invokeAgent(input: InvokeInput, deps: AgentRuntimeDeps): P | |
| reason: message, | ||
| promptTokens: null, | ||
| completionTokens: null, | ||
| totalTokens: null, | ||
| reasoningTokens: null, | ||
| costUsd: null, | ||
| costSource: null, | ||
| durationMs: null | ||
| }; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -141,6 +141,10 @@ export const AgentCallSchema = z.object({ | |
| rawResponse: z.string().nullable().default(null), | ||
| promptTokens: z.number().int().nonnegative().nullable().default(null), | ||
| completionTokens: z.number().int().nonnegative().nullable().default(null), | ||
| totalTokens: z.number().int().nonnegative().nullable().default(null), | ||
| reasoningTokens: z.number().int().nonnegative().nullable().default(null), | ||
| costMicros: z.number().int().nonnegative().nullable().default(null), | ||
| costSource: z.enum(['provider', 'provider_upstream', 'estimated']).nullable().default(null), | ||
|
Comment on lines
+144
to
+147
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| durationMs: z.number().int().nonnegative().nullable().default(null), | ||
| status: AgentCallStatusSchema, | ||
| reasoningEffort: ReasoningEffortSchema.nullable().default(null), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Do not mark an absent cost as estimated.
When
result.costUsdis absent, Line 272 storesNULL, but Line 273 stores'estimated'.OpenAIProviderdoes not provide a cost value, so successful OpenAI calls persist a null cost with an estimated source. The same fallback also reaches the invoke result andnpc_actionpayload.Set
costSourcetonullunless code calculates a non-null localcostUsd. If local estimation is intended, calculate and persist the cost and source as one pair.As per coding guidelines, “Database is source of truth.”
🤖 Prompt for AI Agents
Source: Coding guidelines