Q66: usage counts instructions, tool definitions and tool calls

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-08 11:29:10 -04:00
1 parent c2f8e656f6
commit d184e82e2a
5 files changed
+64 -15

No files matched your search

@@ -19,6 +19,7 @@ import { ProviderRateLimitRegistry } from "./provider-rate-limit-registry";
import { ProviderRequestStore, type ProviderBackgroundRequestRow } from "./provider-request-store";
import { ProviderResponseObserver } from "./provider-response-observer";
import { hashPrompt, readProviderFailureMessage, requireBackgroundText } from "./provider-response-utils";
import { promptChars } from "./spend";
const AUTH_STATE_CONFIGURED = "configured";
const HEALTH_STATE_HEALTHY = "healthy";
@@ -60,7 +61,7 @@ export class ProviderBackgroundService {
model: execution.provider.model,
workspaceId: execution.workspaceId,
promptSha256: hashPrompt(execution.prompt),
inputCharCount: execution.prompt.length,
inputCharCount: promptChars(execution.prompt, execution.instructions),
executionMode: PROVIDER_EXECUTION_MODE.BACKGROUND,
createdAt: new Date().toISOString(),
});
@@ -99,7 +100,7 @@ export class ProviderBackgroundService {
requestId,
executionMode: PROVIDER_EXECUTION_MODE.BACKGROUND,
startedAt,
inputCharCount: execution.prompt.length,
inputCharCount: promptChars(execution.prompt, execution.instructions),
error,
});
throw error;
+14 -11
View File
@@ -17,7 +17,7 @@ import { type AgentTurnMessage, type AgentTurnResult, type ToolDefinition, type
import { selectToolTurn, supportsToolTurn } from "./provider-tool-turns/router";
import { resolveTurnCredential } from "./provider-tool-turns/credential-resolver";
import { currentRunId } from "./run-control";
import { budgetBlock, isPriced, providerPrices } from "./spend";
import { budgetBlock, isPriced, promptChars, providerPrices } from "./spend";
import {
HEALTH_STATE_HEALTHY,
WORKSPACE_SCOPES,
@@ -86,7 +86,7 @@ export class ProviderResponseRunner {
});
this.deps.requestStore.settleUsage({
orgId: input.orgId, requestId, provider: execution.provider, rawResponseJson: result.rawResponseJson,
inputCharCount: execution.prompt.length, outputCharCount: result.text.length,
inputCharCount: promptChars(execution.prompt, execution.instructions), outputCharCount: result.text.length,
});
return requestId;
} catch (error) {
@@ -96,7 +96,7 @@ export class ProviderResponseRunner {
requestId,
executionMode: execution.executionMode,
startedAt,
inputCharCount: execution.prompt.length,
inputCharCount: promptChars(execution.prompt, execution.instructions),
error,
});
throw error;
@@ -129,7 +129,8 @@ export class ProviderResponseRunner {
const blocked = agent ? budgetBlock(this.deps.db, agent.id, isPriced(providerPrices(this.deps.db, provider.id)), new Date()) : null;
if (blocked) throw new Error(blocked);
const credential = await resolveTurnCredential({ authBroker: this.deps.authBroker, integrationKeys: this.deps.integrationKeys, orgId: input.orgId, provider });
const promptChars = JSON.stringify(input.messages).length;
// Everything the model reads: instructions, the conversation, and the tool definitions.
const turnInputChars = (input.instructions?.length ?? 0) + JSON.stringify(input.messages).length + JSON.stringify(input.tools).length;
const requestId = this.deps.requestStore.create({
agentId: agent?.id ?? null,
taskRunId,
@@ -139,19 +140,21 @@ export class ProviderResponseRunner {
model,
workspaceId,
promptSha256: hashPrompt(JSON.stringify(input.messages)),
inputCharCount: promptChars,
inputCharCount: turnInputChars,
executionMode: PROVIDER_EXECUTION_MODE.FOREGROUND,
createdAt: new Date().toISOString(),
});
const startedAt = Date.now();
try {
const result = await turn({ credential, model, instructions: input.instructions, messages: input.messages, tools: input.tools });
// Tool calls are output the model wrote, as much as its text.
const outputChars = result.text.length + (result.toolCalls.length > 0 ? JSON.stringify(result.toolCalls).length : 0);
this.deps.requestStore.markSucceeded({
orgId: input.orgId,
requestId,
responseText: result.text,
rawResponseJson: result.rawResponseJson,
outputCharCount: result.text.length,
outputCharCount: outputChars,
externalResponseId: readProviderResponseId(result.rawResponseJson),
completedAt: new Date().toISOString(),
});
@@ -165,7 +168,7 @@ export class ProviderResponseRunner {
});
this.deps.requestStore.settleUsage({
orgId: input.orgId, requestId, provider: owner, rawResponseJson: result.rawResponseJson,
inputCharCount: promptChars, outputCharCount: result.text.length,
inputCharCount: turnInputChars, outputCharCount: outputChars,
});
return { ...result, requestId };
} catch (error) {
@@ -175,7 +178,7 @@ export class ProviderResponseRunner {
requestId,
executionMode: PROVIDER_EXECUTION_MODE.FOREGROUND,
startedAt,
inputCharCount: promptChars,
inputCharCount: turnInputChars,
error,
});
throw error;
@@ -253,7 +256,7 @@ export class ProviderResponseRunner {
});
this.deps.requestStore.settleUsage({
orgId: input.orgId, requestId, provider: execution.provider, rawResponseJson: JSON.stringify(rawEvents),
inputCharCount: execution.prompt.length, outputCharCount: responseText.length,
inputCharCount: promptChars(execution.prompt, execution.instructions), outputCharCount: responseText.length,
});
return requestId;
} catch (error) {
@@ -263,7 +266,7 @@ export class ProviderResponseRunner {
requestId,
executionMode: execution.executionMode,
startedAt,
inputCharCount: execution.prompt.length,
inputCharCount: promptChars(execution.prompt, execution.instructions),
error,
});
throw error;
@@ -332,7 +335,7 @@ export class ProviderResponseRunner {
model: execution.provider.model,
workspaceId: execution.workspaceId,
promptSha256: hashPrompt(execution.prompt),
inputCharCount: execution.prompt.length,
inputCharCount: promptChars(execution.prompt, execution.instructions),
executionMode: execution.executionMode,
createdAt: new Date().toISOString(),
});
+5
View File
@@ -50,6 +50,11 @@ export function providerPrices(db: DatabaseSync, providerId: string): TokenPrice
return { inputCentsPerMtok: row.input_cents_per_mtok, cachedInputCentsPerMtok: row.cached_input_cents_per_mtok, outputCentsPerMtok: row.output_cents_per_mtok };
}
/** Characters a call sends: the instructions count as much as the prompt. */
export function promptChars(prompt: string, instructions: string | null): number {
return prompt.length + (instructions?.length ?? 0);
}
export function monthStartIso(now: Date): string {
return new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1)).toISOString();
}