-
Notifications
You must be signed in to change notification settings - Fork 1
fix: preserve caller generation budgets through Responses forwarding #1154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
docs/planning/adrs/0132-remove-unsourced-generation-token-ceiling.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| --- | ||
| id: "0132" | ||
| title: "Remove the unsourced shared generation-token ceiling" | ||
| status: accepted | ||
| proposed_date: "2026-09-17" | ||
| accepted_date: "2026-09-17" | ||
| deciders: | ||
| - "repository maintainer" | ||
| affected_components: | ||
| - "contextual_orchestrator/server.py" | ||
| related: | ||
| - path: "docs/planning/adrs/0040-streamed-responses-usage-boundary.md" | ||
| relation: related | ||
| success_criteria: | ||
| - metric: "no shared ingress ceiling" | ||
| target: "Chat, Completions, and Responses accept positive generation budgets without a gateway-wide numeric hard cap" | ||
| source: "tests/test_generation_token_ingress.py" | ||
| - metric: "Responses alias honesty" | ||
| target: "Responses normalizes max_output_tokens over max_completion_tokens over max_tokens and forwards the canonical field" | ||
| source: "tests/test_generation_token_ingress.py" | ||
| - metric: "model-specific limits remain" | ||
| target: "provider-published agent.max_output_tokens and client effective caps still bound generation" | ||
| source: "existing model-budget and client-boundary tests" | ||
| --- | ||
|
|
||
| # Remove the unsourced shared generation-token ceiling | ||
|
|
||
| ## Context | ||
|
|
||
| Ingress validators for Completions `max_tokens`, Chat | ||
| `max_completion_tokens`, and Responses `max_output_tokens` rejected any | ||
| positive budget above `1_048_576`. That number entered the tree as a | ||
| "practical gateway hard ceiling" without a cited provider catalog limit, | ||
| operator policy, or verified product requirement (issue #1151). HTTP tests | ||
| proved rejection, not provenance. Numerical execution limits in this gateway | ||
| must derive from a provider/model constraint, an approved operator policy, or | ||
| a verified product requirement. | ||
|
|
||
| Separately, Responses accepted the legacy aliases but did not always keep a | ||
| canonical `max_output_tokens` value on the body used for provider forwarding, | ||
| so caller budgets could disappear after validation. | ||
|
|
||
| ## Decision | ||
|
|
||
| Remove the shared `1_048_576` ingress ceiling. Keep positive-integer (and | ||
| coercion) validation. Keep model-specific and client `effective_max_output_tokens` | ||
| enforcement. Do not replace the removed number with another arbitrary | ||
| gateway-wide constant, and do not silently clamp. | ||
|
|
||
| On `/v1/responses`, normalize generation budgets with precedence | ||
| `max_output_tokens` > `max_completion_tokens` > `max_tokens` (falling through | ||
| nulls) onto `max_output_tokens` before provider forwarding. | ||
|
|
||
| ## Consequences | ||
|
|
||
| - Callers may request generation budgets larger than the former shared ceiling; | ||
| providers and model metadata still reject or bound physically unsupported | ||
| sizes. | ||
| - Operators who need a gateway-wide cap must introduce an explicit, sourced | ||
| policy (KV/operator config or catalog-derived limit), not an unsourced | ||
| constant in the validator. | ||
| - Regression coverage uses a boundary fixture above the former cap only to | ||
| prove passthrough; it does not claim any live model supports that size. | ||
|
|
||
| ## References | ||
|
|
||
| OpenAI. (n.d.). *Create a model response | OpenAI API reference* | ||
| (`max_output_tokens`). Retrieved September 17, 2026, from | ||
| https://platform.openai.com/docs/api-reference/responses/create | ||
|
|
||
| OpenAI. (n.d.). *Chat Completions | OpenAI API reference* | ||
| (`max_completion_tokens`, `max_tokens`). Retrieved September 17, 2026, from | ||
| https://platform.openai.com/docs/api-reference/chat/create | ||
|
|
||
| Issue #1151 — Validate or replace the unsubstantiated common generation-token | ||
| ceiling. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| """Generation limits remain caller values at the model execution boundary.""" | ||
|
|
||
| import http.client | ||
| import json | ||
| import threading | ||
|
|
||
| import pytest | ||
|
|
||
| from contextual_orchestrator import ModelAgent, TaskOrchestrator | ||
| from contextual_orchestrator.orchestrator import ModelClient | ||
| from contextual_orchestrator.server import SecurityConfig, build_server | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "endpoint_path, budget_field, input_fields", | ||
| [ | ||
| ("/v1/completions", "max_tokens", {"prompt": "hello"}), | ||
| ("/v1/completions", "max_completion_tokens", {"prompt": "hello"}), | ||
| ("/v1/chat/completions", "max_tokens", {"messages": [{"role": "user", "content": "hello"}]}), | ||
| ("/v1/chat/completions", "max_completion_tokens", {"messages": [{"role": "user", "content": "hello"}]}), | ||
| ("/v1/responses", "max_tokens", {"input": "hello"}), | ||
| ("/v1/responses", "max_completion_tokens", {"input": "hello"}), | ||
| ("/v1/responses", "max_output_tokens", {"input": "hello"}), | ||
| ("/v1/responses", "max_output_tokens", {"input": "hello", "max_completion_tokens": 32, "max_tokens": 16}), | ||
| ("/v1/responses", "max_completion_tokens", {"input": "hello", "max_output_tokens": None, "max_tokens": 16}), | ||
| ("/v1/responses", "max_tokens", {"input": "hello", "max_output_tokens": None, "max_completion_tokens": None}), | ||
| ], | ||
| ) | ||
| @pytest.mark.parametrize("requested_limit", [64, 1_048_577]) | ||
| def test_http_preserves_caller_generation_limit(monkeypatch, endpoint_path, budget_field, input_fields, requested_limit): | ||
| """A boundary fixture above the former cap does not claim real model capacity.""" | ||
| model_client = ModelClient() | ||
| observed_limits = [] | ||
| original_mock = model_client._mock | ||
| original_raw_mock = model_client._mock_raw | ||
|
|
||
| def observe_mock(model_agent, *call_args, **call_kwargs): | ||
| """Record the budget effective at a mocked model invocation.""" | ||
| observed_limits.append(model_client.effective_max_output_tokens(model_agent)) | ||
| return original_mock(model_agent, *call_args, **call_kwargs) | ||
|
|
||
| def observe_raw_mock(model_agent, provider_endpoint, provider_payload): | ||
| """Check the canonical Responses budget at the provider boundary.""" | ||
| assert provider_endpoint.strip("/") == "responses" | ||
| assert "max_tokens" not in provider_payload | ||
| assert "max_completion_tokens" not in provider_payload | ||
| observed_limits.append(provider_payload.get("max_output_tokens")) | ||
| return original_raw_mock(model_agent, provider_endpoint, provider_payload) | ||
|
|
||
| monkeypatch.setattr(model_client, "_mock", observe_mock) | ||
| monkeypatch.setattr(model_client, "_mock_raw", observe_raw_mock) | ||
| task_orchestrator = TaskOrchestrator( | ||
| [ModelAgent("general_agent", "mock-planner", tags=("reasoning", "writing"))], | ||
| client=model_client, | ||
| ) | ||
| http_server = build_server( | ||
| task_orchestrator, port=0, security=SecurityConfig(auth_token="fixture-token") | ||
| ) | ||
| server_thread = threading.Thread(target=http_server.serve_forever, daemon=True) | ||
| server_thread.start() | ||
| http_connection = http.client.HTTPConnection(*http_server.server_address, timeout=10) | ||
| try: | ||
| request_payload = {"model": "mock-planner", **input_fields, budget_field: requested_limit} | ||
| http_connection.request( | ||
| "POST", endpoint_path, json.dumps(request_payload), | ||
| {"Content-Type": "application/json", "Authorization": "Bearer fixture-token"}, | ||
| ) | ||
| http_response = http_connection.getresponse() | ||
| response_payload = json.loads(http_response.read()) | ||
| assert http_response.status == 200, response_payload | ||
| assert observed_limits and all(observed_limit == requested_limit for observed_limit in observed_limits), observed_limits | ||
| finally: | ||
| http_connection.close() | ||
| http_server.shutdown() | ||
| server_thread.join(timeout=5) | ||
| http_server.server_close() | ||
| task_orchestrator.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.