@tanstack/openai-base: Responses usage drops input_tokens_details.cache_write_tokens
Los mantenedores suelen responder en 1 día
@tombeckenham ya está trabajando en esto.
Desde el 13/10/2026.
Evaluación
Este issue todavía no se ha evaluado.
Descripción
Package / version: @tanstack/openai-base 0.13.1 via @tanstack/ai-openai 0.29.1, @tanstack/ai 0.68.0, openai 7.32.0, Node 24.20.0. The same code is on main.
Summary
buildResponsesUsage in packages/openai-base/src/usage.ts reads input_tokens_details.cached_tokens and ignores input_tokens_details.cache_write_tokens. A Responses API call that writes the prompt cache reports the written count, and OpenAI prices cache writes separately from uncached input on current models, but the number never reaches TokenUsage.promptTokensDetails.cacheWriteTokens. Anything that meters spend from usage under-counts every call that primes a cache.
buildChatCompletionsUsage in the same file already maps the field (prompt_tokens_details.cache_write_tokens → cacheWriteTokens), so the two paths disagree. The openai SDK types cache_write_tokens as a required member of ResponseUsage.input_tokens_details.
Reproduction
import { chat } from '@tanstack/ai'
import { createOpenaiChat } from '@tanstack/ai-openai'
import OpenAI from 'openai'
const MODEL = 'gpt-6-luna'
// A system prompt past the 1024-token caching floor. A fresh nonce makes each prompt a first
// sighting, so the call writes the cache instead of reading it.
const filler = 'The quarterly report covers revenue, margin, churn, headcount, and regional growth across every business unit. '
const fresh = () => `You answer in one word. Session ${crypto.randomUUID()}.\n\n${filler.repeat(90)}`
const question = 'What colour is the sky on a clear day?'
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY })
const raw = await client.responses.create({ model: MODEL, instructions: fresh(), input: question })
console.log('raw Responses usage, fresh prompt =', JSON.stringify(raw.usage))
const adapter = createOpenaiChat(MODEL, process.env.OPENAI_API_KEY)
for await (const c of chat({ adapter, systemPrompts: [fresh()], messages: [{ role: 'user', content: question }] })) {
if (c.type === 'RUN_FINISHED') console.log('RUN_FINISHED usage, fresh prompt =', JSON.stringify(c.usage))
}
Observed
raw Responses usage, fresh prompt = {"input_tokens":1941,"input_tokens_details":{"cache_write_tokens":1938,"cached_tokens":0},"output_tokens":5,"output_tokens_details":{"reasoning_tokens":0},"total_tokens":1946}
RUN_FINISHED usage, fresh prompt = {"promptTokens":1944,"completionTokens":18,"totalTokens":1962,"completionTokensDetails":{"reasoningTokens":11}}
OpenAI reports 1938 tokens written to the cache. The RUN_FINISHED usage has no promptTokensDetails at all.
A second call with the same prompt comes back with cached_tokens: 1914 and the adapter does surface that one as promptTokensDetails.cachedTokens, so only the write side is missing.
Expected
usage.promptTokensDetails.cacheWriteTokens equal to input_tokens_details.cache_write_tokens, as the Chat Completions path does.
Cause and fix
buildResponsesUsage only copies cached_tokens (packages/openai-base/src/usage.ts, around line 111 on main). I'm carrying this as a local patch to the built file, the same shape as the cachedTokens block above it, in case it saves whoever picks this up a minute:
const cacheWriteTokens = usage.input_tokens_details?.cache_write_tokens
if (cacheWriteTokens && cacheWriteTokens > 0) {
result.promptTokensDetails = {
...result.promptTokensDetails,
cacheWriteTokens,
}
}
Do you intend to try to help solve this bug with your own PR? No, because I do not have time to dig into it. The hunk above is the fix as far as I can tell; I won't be sending a PR.
- I agree to follow this project's Code of Conduct
- I understand that if my bug cannot be reliably reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
- Lenguaje dominante
- TypeScript
- Estrellas
- 3.2k
- Forks
- 361
- Merge medio
- 1 d 15 h
- PR fusionados (30 d)
- 221
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de TanStack/ai
-
fix(ai-anthropic): chat({ reasoning }) on Claude 4.6 sends a top-level effort and gets a 400Posiblemente ocupada @tombeckenham la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 20/100
Los mantenedores suelen responder en 1 día
-
@tanstack/ai-anthropic: a stream that fails after message_start loses the call's input usagePosiblemente ocupada @AlemTuzlak la tomó hoy. Abiertowaiting-on: maintainer
TanStack/ai#1735 · 1 asignado ·
Los mantenedores suelen responder en 1 día
-
summarize() sends temperature 0.3 to models that reject sampling parameters (e.g. claude-sonnet-5-5)Posiblemente ocupada @jherr la tomó hace 3 días. Abiertohas-pr waiting-on: maintainer
TanStack/ai#1703 · 1 comentario · 1 asignado ·
Los mantenedores suelen responder en 1 día
-
ai-openrouter: malformed Chat Completions tool arguments execute as an empty objectPosiblemente ocupada @tombeckenham la tomó hace 4 días. Abiertohas-pr waiting-on: maintainer
TanStack/ai#1689 · 1 asignado ·
Los mantenedores suelen responder en 1 día
-
StreamProcessor discards metadata from REASONING_MESSAGE_STARTPosiblemente ocupada @tombeckenham la tomó hace 4 días. Abiertowaiting-on: maintainer
TanStack/ai#1667 · 1 asignado ·
Los mantenedores suelen responder en 1 día
Todos los issues de TanStack/ai
Issues similares
-
documentation good first issue hacktoberfest help wanted
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
libredb/libredb-studio#1756 ·
Los mantenedores suelen responder en 1 día
-
state:triage-needed
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
Los mantenedores suelen responder en 1 día
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
Dificultad 1/5 Menos de una hora Aptitud para principiantes 72/100
lingdojo/kana-dojo#32365 · 1 comentario · 5 reacciones ·
Los mantenedores suelen responder en 1 día
-
friction
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
kentcdodds/kody#3706 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
copperheadhq/copperhead#378 ·
Los mantenedores suelen responder en 1 día