* Attribute a refused fallback attempt's token usage to the refusing model * before the fallback swap wipes per-attempt counters. The toolModelUsages * entry is the durable record (session-usage rebuilds scan it per-model); the * live recordSessionUsage call keeps in-memory session totals c
(
workspaceId: WorkspaceId,
streamInfo: WorkspaceStreamInfo,
refusedModel: string
)
| 2034 | * live recordSessionUsage call keeps in-memory session totals consistent with it. |
| 2035 | */ |
| 2036 | private async recordRefusedAttemptUsage( |
| 2037 | workspaceId: WorkspaceId, |
| 2038 | streamInfo: WorkspaceStreamInfo, |
| 2039 | refusedModel: string |
| 2040 | ): Promise<void> { |
| 2041 | const { usage, providerMetadata } = await this.getRefusalUsageSnapshot(streamInfo); |
| 2042 | if (!usage) { |
| 2043 | return; |
| 2044 | } |
| 2045 | |
| 2046 | streamInfo.toolModelUsages.push({ |
| 2047 | toolName: "model_fallback_refusal", |
| 2048 | timestamp: Date.now(), |
| 2049 | model: refusedModel, |
| 2050 | metadataModel: streamInfo.metadataModel, |
| 2051 | usage, |
| 2052 | ...(providerMetadata ? { providerMetadata } : {}), |
| 2053 | }); |
| 2054 | |
| 2055 | await this.recordSessionUsage( |
| 2056 | workspaceId, |
| 2057 | refusedModel, |
| 2058 | usage, |
| 2059 | providerMetadata, |
| 2060 | "Failed to record refused-attempt session usage (fallback unaffected)", |
| 2061 | "warn", |
| 2062 | streamInfo |
| 2063 | ); |
| 2064 | } |
| 2065 | |
| 2066 | private async recordTerminalRefusalUsage( |
| 2067 | workspaceId: WorkspaceId, |
no test coverage detected