Skip to content

server: POST /v1/responses/input_tokens counts the prompt POST /v1/responses would prefill, generating nothing - #3537

Draft
ashhart wants to merge 5 commits into
mainfrom
lead/responses-input-tokens
Draft

ashhart wants to merge 5 commits into
mainfrom
lead/responses-input-tokens

Conversation

@ashhart

@ashhart ashhart commented Nov 16, 2026

Copy link
Copy Markdown
Owner

OpenAI's Responses API has POST /v1/responses/input_tokens, which counts the input tokens a request would use without generating. On TensorFold the path answered 404 "unknown path". This serves it with the exact count the run would prefill.

What this changes

  • The route: POST /v1/responses/input_tokens (and /responses/input_tokens) takes the same body as POST /v1/responses: model, input, instructions, tools, previous_response_id and so on. It answers {"object": "response.input_tokens", "input_tokens": N} and generates nothing.

  • How N is computed: the body goes through the Responses route's own reader, scope check, translation and logprobs check, now shared. Then it goes through openai_prepare.prepare, the run's own render and checks, which the WebSocket warmup already uses. So N is the exact prompt length /v1/responses would prefill for the same body.

    • In the goldens, the five counts equal those cases' usage.input_tokens (26, 26, 162, 38, 77).
    • The stored-parent chain gives 62 = 62.
  • Refusals: a body /v1/responses refuses gets the same status and error. All 16 refusal cases are byte-identical to the Responses route's. A streamed body's refusal comes back as a JSON reply.

  • Two modes can't be counted, and say so: they rewrite the prompt during the run with text that doesn't exist yet. A request either one would take gets a 400 naming the flag:

    • --compact-at past its threshold, where the model writes the compaction note;
    • a --cortheon turn, which adds its program, tools and research.

    cortheon.begin's checks moved into cortheon.takes, so both callers use the same checks, with no behaviour change.

  • Keys, scopes, admission and the request gate: the Responses route's. Counts are not written to TENSORFOLD_REQUEST_LOG.

  • Issue: n/a. This came from the 16 Nov check of competitor gap docs against main (llama.cpp serves it; the /v1/responses docs here listed it as missing); no issue or PR covered it.

  • Stacked on: server: every reply carries X-Request-Id, a client's own valid id echoed and in the request's log lines #3536 (X-Request-Id), which stacks on server: GET /v1/models/{id} returns one served model's entry, and an unknown id is a 404 model_not_found #3535 (GET /v1/models/{id}). This PR adds ece2df75d6. The three PRs touch the security-case list, the key-scope docs row and root.zig, and the stack resolves those once.

  • atc overlap --json: to run after review. golden/cases.json, which is generated, also changes in the code-infill draft server: /v1/completions takes suffix, a fill-in-the-middle prompt from the tokenizer's own FIM tokens #3528; whichever lands second reruns freeze_cases.py.

Checks on this head

  • Head commit: ece2df7 on 7ccdedb (main e7e2424). Its tree equals the one the checks ran on, apart from CHANGELOG lines.
  • Lean checker: 337 on base and head, 0 new. responses.zig is exactly 600 lines.
  • tools/pr_check.py: 0 errors, 2 advisory warnings (the inherited 0.0.0.0 in the regenerated security-case list, and the PR number).
  • Tests:
    • On main: "1478 pass, 9 skip, 4 fail", and goldens 322/357 with 22 unexpected.
    • On the whole stack: zig build test-server-cpu 1494/1503 (9 skipped); zig build test-golden 344/357 equal with 0 unexpected (the 13 are main's existing known list), and security 80/80; zig build test-model-discovery-host test-key-scopes-host 76/76.
    • zig fmt --check, git diff --check: clean.
  • CI: pending.
  • Not run: image-token counts, because the fake engine has no vision (the count path does include image expansion).

Receipt

n/a: a counting route that renders the prompt and generates nothing. Host checks are above.

Checklist

  • The title and description match the head commit.

  • New tests fail before the change, pass after it, and skip cleanly without their dependency.

  • Comments and docstrings are one line. No measurements or history in the source.

  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

  • Synthena (Ash's Agent)

…oed and in the request's log lines

A client could not tie a reply, or the server's log lines for it, to its own
trace, and a streaming client had no cancel handle until its first event.
Each reply head, an SSE stream's included, now carries X-Request-Id after
Date.

A request with exactly one X-Request-Id of 1 to 128 printable ASCII
characters and no space gets it back, and every [tensorfold] line its thread
writes while it handles that request, the access line first, ends with
request_id=ID. Any other value, an empty one or a repeated header is
ignored, never refused. The reply then carries the server's own id: the one
POST /v1/requests/{id}/cancel takes, named where openai.runNamed makes it,
so chat completions, completions and Responses replies carry the chatcmpl-,
cmpl- or resp_ id their body has, and /v1/messages its chat completion's
chatcmpl- id. A request that names none gets a new req_ id of 32 hex
digits; ids.fill makes it into the connection's own buffer. Bodies are
unchanged.

The golden normalizer drops a server-made X-Request-Id as it drops Date, so
every existing golden compares as before; a new request-id group pins the
echo (on a 401, a 400, a 404 and a stream) and the values that are not
echoed. request_id_test.zig covers the echo and log lines on a connection,
the fresh id, the keep-alive reset, and over sockets the id each route
carries. The Ollama keep-alive test now looks for a 501 status line, since a
random id can hold those digits.
…sponses would prefill, generating nothing

OpenAI's input-token count for a Responses request answered "unknown path".
POST /v1/responses/input_tokens (and /responses/input_tokens) now takes the
same body and answers {"object": "response.input_tokens", "input_tokens": N}.

The body goes through the route's own reader, scope check and translation
(responses.request, split out of responses.post), the turn's logprobs check
(responses.unofferedLogprobs, split out of responses.turn), then
openai_prepare.prepare: the run's own plan and chat.prepare, as the Responses
WebSocket warmup already counts its prompt. So N is the rendered prompt the
run prefills, with instructions, tools, items, a stored parent's
conversation and image tokens, and a body /v1/responses refuses gets the same
status and error. The request gate, admission and key scopes are the
Responses route's; inference keys reach it, read keys do not.

A count is not written to TENSORFOLD_REQUEST_LOG: prepare takes the run's
skip_request_log flag, and the warmup passes false as before. Two modes
rewrite a prompt during the run with text not yet written, so a request
they would take is refused by name: --compact-at past its threshold, using
compact.zig's own over and wanted, and a --cortheon turn, using
cortheon.takes, the check begin made before it starts one.

Golden cases on a server of their own count five Responses corpus bodies
(each equal to that case's usage.input_tokens), refuse its sixteen refusal
bodies with byte-identical replies, and count a stored parent's continuation
as its run prefills it. responses_count_test.zig checks counts against the
prompts a recording engine is given, the refusals and scope rules, both mode
refusals and the request log. All fail on main and pass here.
@ashhart
ashhart force-pushed the lead/responses-input-tokens branch from 2093c65 to 4c3ef0f Compare November 22, 2026 04:59

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant