feat(api): native Responses input token count (STRA-77) (#19)

* feat(api): count Responses input tokens without inference

* fix(api): box shared request validation errors
This commit is contained in:
Pavlo Ratushnyi
2026-10-05 02:26:00 +02:00
committed by GitHub
parent 4fc0d4b3b0
commit 42eaa5dae4
5 changed files with 350 additions and 65 deletions
+27
View File
@@ -21,6 +21,33 @@ failed or draining and does not acquire a generation slot or touch model files.
Use `/ready` to check inference availability. Listing does not download, start,
test or certify the model, and does not expose filesystem paths or credentials.
## Input token counting
`POST /v1/responses/input_tokens` returns
`{"object":"response.input_tokens","input_tokens":123}` using the same native
history expansion, prompt template and tokenizer as generation. The shape follows
the [Responses input token count endpoint](https://developers.openai.com/api/reference/resources/responses/subresources/input_tokens/methods/count).
The local subset requires `model` and `input` and accepts the supported create-request
fields; unknown fields and unsupported live features are rejected. Instructions,
reasoning, text format and tool definitions contribute exactly as they do to the
rendered generation prompt. `previous_response_id` includes the saved transcript
and new input; new instructions replace prior instructions. Missing, deleted or
evicted parents return `404 response_not_found`.
Counting requires a configured tokenizer, but does not require a ready worker,
queue inference, create a response ID or modify history (`store` has no effect).
It always returns ordinary JSON, including when `stream: true` is supplied.
Generation-only settings are validated but do not generate output. The count is
for the full rendered prompt, not only newly evaluated tokens or a prediction of
cache reuse, output length, model quality or available context space. It does not
truncate input or guarantee the request fits the worker's context.
The route shares optional bearer auth, the 16 MiB body limit and bounded HTTP
admission with create: drain returns 503 and exhausted admission returns 429.
It remains available during worker failure when the service is not draining.
The isolated CPU test compares counts against actual `GEN` token IDs, with exact
integer equality; this does not qualify real-model inference.
## Engine activity metrics
`GET /metrics` returns JSON (`schema_version: 1`), protected by the same optional