Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

fix(responses,shared): report reasoning_tokens truthfully (#764) (#829)

suantea (asuan_suan)141023a09da6
diff --git a/server/src/__tests__/routes/responses.test.ts b/server/src/__tests__/routes/responses.test.ts
index 8c2a0c55..aa96deac 100644
--- a/server/src/__tests__/routes/responses.test.ts
+++ b/server/src/__tests__/routes/responses.test.ts
@@ -224,6 +224,29 @@ describe('POST /v1/responses (#96)', () => {
// ttfb must be the reasoning-head moment (well before the text token that
// triggers the commit), not the flush time ≈ latency.
expect(rows[0].ttfb_ms).toBeLessThan(rows[0].latency_ms - 250);
+ // The completed Response reports the thinking tokens truthfully instead of
+ // a hardcoded 0: 'thinking hard…' is 14 chars → ceil(14/4) = 4.
+ const completed = text.split('event: response.completed')[1];
+ const payload = JSON.parse(completed.slice(completed.indexOf('{')));
+ expect(payload.response.usage.output_tokens_details.reasoning_tokens).toBe(4);
+ });
+
+ it('non-stream: reports reasoning_tokens from the provider usage when advertised', async () => {
+ mockRouteRequest.mockReturnValue(fakeRoute({
+ async chatCompletion() {
+ return {
+ id: 'c', object: 'chat.completion', created: 0, model: 'fake-model',
+ choices: [{ index: 0, message: { role: 'assistant', content: 'answer', reasoning_content: 'thinking' }, finish_reason: 'stop' }],
+ usage: { prompt_tokens: 3, completion_tokens: 6, total_tokens: 9, completion_tokens_details: { reasoning_tokens: 2 } },
+ };
+ },
+ async *streamChatCompletion() { /* unused */ },
+ }));
+
+ const { status, text } = await post(app, '/v1/responses', { input: 'hi', stream: false }, key);
+ expect(status).toBe(200);
+ const body = JSON.parse(text);
+ expect(body.usage.output_tokens_details.reasoning_tokens).toBe(2);
});
it('stream: tool-call deltas produce function_call events with assembled arguments', async () => {
diff --git a/server/src/routes/responses.ts b/server/src/routes/responses.ts
index a6e30fe5..99d20569 100644
--- a/server/src/routes/responses.ts
+++ b/server/src/routes/responses.ts
@@ -457,6 +457,7 @@ export function buildResponseObject(opts: {
toolCalls: ChatToolCall[];
promptTokens: number;
completionTokens: number;
+ reasoningTokens?: number;
}) {
const output: any[] = [];
if (opts.text.length > 0) {
@@ -491,7 +492,7 @@ export function buildResponseObject(opts: {
input_tokens: opts.promptTokens,
input_tokens_details: { cached_tokens: 0 },
output_tokens: opts.completionTokens,
- output_tokens_details: { reasoning_tokens: 0 },
+ output_tokens_details: { reasoning_tokens: opts.reasoningTokens ?? 0 },
total_tokens: opts.promptTokens + opts.completionTokens,
},
};
@@ -786,6 +787,10 @@ responsesRouter.post('/responses', async (req: Request, res: Response) => {
// tool-call accumulator keyed by the provider's tool_call index
const toolAcc = new Map<number, { outputIndex: number; itemId: string; callId: string; name: string; args: string }>();
let totalOutputTokens = 0;
+ // #764: thinking tokens are tracked separately so the final Response
+ // object can report `output_tokens_details.reasoning_tokens` truthfully
+ // instead of a hardcoded 0.
+ let totalReasoningTokens = 0;
// #764: ttfb = first token of ANY kind (content or reasoning), recorded
// in the pump loop; commit() only backfills streams that never produced
// one. This path previously logged no ttfb at all, so Analytics showed
@@ -884,6 +889,7 @@ responsesRouter.post('/responses', async (req: Request, res: Response) => {
}
if (text) {
totalOutputTokens += Math.ceil((text.length + reasoning.length) / 4);
+ if (reasoning.length > 0) totalReasoningTokens += Math.ceil(reasoning.length / 4);
if (dialectMode === 'passthrough') {
if (msgItemId === null) openTextItem('');
sse('response.output_text.delta', {
@@ -906,6 +912,7 @@ responsesRouter.post('/responses', async (req: Request, res: Response) => {
} else if (reasoning.length > 0) {
// #764: thinking-only chunk (no visible text yet) — count tokens.
totalOutputTokens += Math.ceil(reasoning.length / 4);
+ totalReasoningTokens += Math.ceil(reasoning.length / 4);
}
// Tool-call deltas → function_call item + argument deltas.
@@ -1034,6 +1041,7 @@ responsesRouter.post('/responses', async (req: Request, res: Response) => {
const finalResponse = buildResponseObject({
id: responseId, model: route.modelId, text: msgText,
toolCalls: finalToolCalls, promptTokens: estimatedInputTokens, completionTokens: totalOutputTokens,
+ reasoningTokens: totalReasoningTokens,
});
sse('response.completed', { response: finalResponse });
res.end();
@@ -1127,6 +1135,10 @@ responsesRouter.post('/responses', async (req: Request, res: Response) => {
// provider omits `usage`.
const completionTokens = result.usage?.completion_tokens
?? Math.ceil((text.length + completionReasoningText(result).length) / 4);
+ // #764: report reasoning_tokens truthfully — the provider's own count
+ // when advertised, else the same chars/4 estimate of the thinking text.
+ const reasoningTokens = result.usage?.completion_tokens_details?.reasoning_tokens
+ ?? Math.ceil(completionReasoningText(result).length / 4);
// Empty completion → fail over via the shared loop (see the streaming
// path); finish_reason 'length' skips the cooldown/penalty.
@@ -1166,7 +1178,7 @@ responsesRouter.post('/responses', async (req: Request, res: Response) => {
setFallbackHeaders(res, attempt, attemptLog);
res.json(buildResponseObject({
id: responseId, model: route.modelId, text, toolCalls,
- promptTokens, completionTokens,
+ promptTokens, completionTokens, reasoningTokens,
}));
traceRouteEvent('Responses', {
diff --git a/shared/types.ts b/shared/types.ts
index 1bc05f37..06384c4d 100644
--- a/shared/types.ts
+++ b/shared/types.ts
@@ -362,6 +362,11 @@ export interface TokenUsage {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
+ // OpenAI-standard breakdown some providers advertise alongside the totals.
+ // Optional because many free-tier endpoints omit it; the proxy falls back
+ // to its chars/4 estimate when absent. (#764)
+ completion_tokens_details?: { reasoning_tokens?: number };
+ prompt_tokens_details?: { cached_tokens?: number };
}
export interface ChatCompletionResponse {