| 240 | } |
| 241 | |
| 242 | func (i *responsesInterceptionBase) recordTokenUsage(ctx context.Context, response *responses.Response) { |
| 243 | if response == nil { |
| 244 | i.logger.Warn(ctx, "got empty response, skipping token usage recording") |
| 245 | return |
| 246 | } |
| 247 | |
| 248 | usage := response.Usage |
| 249 | |
| 250 | // Keeping logic consistent with chat completions |
| 251 | // Input *includes* the cached tokens, so we subtract them here to reflect actual input token usage. |
| 252 | inputNonCacheTokens := usage.InputTokens - usage.InputTokensDetails.CachedTokens |
| 253 | |
| 254 | if err := i.recorder.RecordTokenUsage(ctx, &recorder.TokenUsageRecord{ |
| 255 | InterceptionID: i.ID().String(), |
| 256 | MsgID: response.ID, |
| 257 | Input: inputNonCacheTokens, |
| 258 | Output: usage.OutputTokens, |
| 259 | CacheReadInputTokens: usage.InputTokensDetails.CachedTokens, |
| 260 | ExtraTokenTypes: map[string]int64{ |
| 261 | "input_cached": usage.InputTokensDetails.CachedTokens, // TODO: remove from ExtraTokenTypes (https://github.com/coder/aibridge/issues/243) |
| 262 | "output_reasoning": usage.OutputTokensDetails.ReasoningTokens, |
| 263 | "total_tokens": usage.TotalTokens, |
| 264 | }, |
| 265 | }); err != nil { |
| 266 | i.logger.Warn(ctx, "failed to record token usage", slog.Error(err)) |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // extractModelThoughts extracts model thoughts from response output items. |
| 271 | // It captures both reasoning summary items and commentary messages (message |