mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-12-03 10:27:23 +08:00
feat/https-compose-caddy
2
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
7968122658 |
fix(proxy): stop the 0.39.0 TPM limiter from refusing large-context requests forever (#3806)
## Description #3350 (shipped in 0.39.0) started enforcing the token-per-minute bucket, which has two problems for large-context coding agents. 1. **A request larger than the bucket is refused forever.** The bucket never holds more than `rate_limit_tokens_per_minute` (`refilled_tokens` caps it there), so a request whose input is over the limit can never be admitted. The 429's `Retry-After` never comes true, and retrying, or `/compact`, which sends the same large conversation, hits the same 429 again. Once a Claude Code session passes 100k input tokens it is stuck until the proxy is restarted. 2. **The implicit 100k default is now enforced on every proxy.** Before #3350 the token bucket was never consumed, so no one ran into the default. Now a stock `headroom proxy` / `headroom wrap claude` is a 100k tokens/minute throttle, which a single 1M-context turn exceeds on its own. Users see `{"detail":"Token rate limited. Retry after 79.2s"}` straight after upgrading. Fixes: 1. `consume_from_bucket` admits an oversized request once the bucket is full, then charges it in full. The balance goes negative and later requests wait it off, so the configured average rate still holds. A request that fits in the bucket behaves exactly as before. 2. The token limit is opt-in again: `rate_limit_tokens_per_minute` defaults to `None` (no token limit). `--tpm` / `HEADROOM_TPM` still enforce one, which is what #3347 asked for. The request-per-minute limit (default 60) is unchanged. Follows up #3347 / #3350. ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [ ] Performance improvement - [ ] Code refactoring (no functional changes) ## Changes Made - `rate_limit_policy.consume_from_bucket`: a request larger than the bucket's capacity waits only for a full bucket, then is admitted and charged in full. - `TokenBucketRateLimiter`: `tokens_per_minute=None` never limits and creates no token buckets. - `ProxyConfig.rate_limit_tokens_per_minute`, `headroom proxy --tpm` and the `python -m headroom.proxy.server --tpm` default to `None`. The startup banner prints `unlimited`, and the docs table says so too. - Tests: three policy cases (oversized admitted when full, waits only for a full bucket, debt delays the next request); the four handler integration tests now check that the first oversized request is admitted and charged, and that the second is refused before upstream; the limiter defaults to no token limit; the CLI default is `None`. ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [x] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality - [x] Manual testing performed ### Test Output ```text $ uv run --frozen --extra dev pytest tests/test_rate_limit_policy.py tests/test_rate_limiter.py tests/test_proxy_tpm_enforcement.py tests/test_proxy_config_rate_limit.py tests/test_proxy_openai_rate_limit_key.py tests/test_codex_rate_limits.py tests/test_cli_proxy_env.py -q 139 passed, 1 skipped, 1 warning in 4.37s # The same new policy/integration tests on the 0.39.0 consume_from_bucket: $ uv run --frozen --extra dev pytest tests/test_rate_limit_policy.py tests/test_proxy_tpm_enforcement.py -q 6 failed, 6 passed, 1 warning in 2.56s $ uvx ruff check <changed files> All checks passed! $ uvx ruff format --check <changed files> 6 files already formatted $ uv run --frozen --extra dev mypy headroom/proxy/rate_limiter.py headroom/proxy/models.py headroom/cli/proxy.py headroom/proxy/server.py Success: no issues found in 4 source files ``` ## Real Behavior Proof - Environment: macOS 15 (Darwin 24.6.0), Python 3.12. Stock headroom-ai 0.39.0 wheel on port 18902, this branch on port 18901, both `headroom proxy --no-optimize` with default settings. - Exact command / steps: POSTed the same ~435k-input-token `/v1/messages` request (claude-haiku-4-5, invalid API key so the upstream answers 401 instead of running it) twice to each proxy, then repeated against this branch started with `--tpm 100000`. - Observed result: Stock 0.39.0: 429 `Token rate limited. Retry after 200.8s` on both tries, the second in 6 ms, and the same on every retry after that. This branch, default settings: 401 from Anthropic on both tries (forwarded, no token limit). This branch with `--tpm 100000`: the first try was forwarded (401 from Anthropic) and the second got 429 `Retry after 260.6s`, the wait for the 335k-token debt plus a full bucket. That wait now actually ends. - Not tested: A real paid 1M-context session against Anthropic (the upstream 401 is enough to prove the proxy forwarded the request); Gemini and OpenAI Responses live (covered by the handler integration tests only). ## Runtime Rollout Safety - Rollout-managed feature(s): None. - Minimum rollout channel: Stable. - Stable/default behavior changed: Yes. The default token-per-minute limit goes back to unlimited, as it effectively was before 0.39.0; an explicit `--tpm` / `HEADROOM_TPM` still enforces one. - Kill switch / disable path: Set `--tpm` / `HEADROOM_TPM` to restore a token limit; `--no-rate-limit` disables rate limiting entirely. - Unsafe override required: No. - Qualification impact: None. - Rollback path: Revert this PR. ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [x] I did **not** edit `CHANGELOG.md` — it is generated by release-please from my Conventional Commit PR title (a CI guard enforces this) ## Screenshots (if applicable) N/A ## Additional Notes The two commits can be taken separately. The first alone fixes the permanent 429 for anyone who sets `--tpm`. The second restores the pre-0.39.0 default. Headroom Desktop is working around this for now by launching the proxy with `--no-rate-limit`. --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> |
||
|
|
138736c96f |
fix(proxy): enforce configured TPM limits (#3350)
## Description Consume the configured token-per-minute bucket after input token counting and before upstream dispatch. Previously handlers checked only the request-per-minute bucket, so `rate_limit_tokens_per_minute` created a token bucket that was never used. Closes #3347 ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [ ] Performance improvement - [ ] Code refactoring (no functional changes) ## Changes Made - Enforce input TPM in Anthropic Messages. - Enforce input TPM in OpenAI Chat Completions and Responses. - Enforce input TPM in Gemini generate-content requests. - Return 429 before upstream dispatch and record rate-limit metrics. - Add integration coverage for all four handler paths. ## Testing - [x] Unit tests pass (`pytest`) - [ ] Linting passes (`ruff check .`) - [ ] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality - [x] Manual testing performed ### Test Output ```text $ .venv/bin/pytest -q tests/test_proxy_tpm_enforcement.py 4 passed, 1 warning in 1.48s $ .venv/bin/pytest -q tests/test_proxy_tpm_enforcement.py tests/test_proxy_config_rate_limit.py tests/test_cli_proxy_env.py -k 'tpm or rate_limit' 11 passed, 67 deselected, 1 warning $ .venv/bin/ruff check <changed files> All checks passed! $ .venv/bin/ruff format --check <changed files> 4 files already formatted $ .venv/bin/mypy headroom/proxy/handlers/anthropic.py headroom/proxy/handlers/openai.py headroom/proxy/handlers/gemini.py Success: no issues found in 3 source files ``` ## Real Behavior Proof - Environment: Linux, Python 3.14, real FastAPI proxy app and tokenizer, real local HTTP upstream, `rate_limit_tokens_per_minute=1`. - Exact command / steps: started an allowlisted local Anthropic-compatible HTTP server with a request counter, configured the proxy with TPM 1 and RPM 1000, then sent a normal `/v1/messages` request containing more than one input token. - Observed result: the proxy returned HTTP 429 before making any upstream request. ```text HTTP Request: POST http://testserver/v1/messages "HTTP/1.1 429 Too Many Requests" status=429 detail=Token rate limited. Retry after 1200.0s upstream_calls=0 ``` - Not tested: external/cloud providers; streaming-specific response behavior; distributed rate-limit backends; full repository test suite. ## Runtime Rollout Safety - Rollout-managed feature(s): proxy rate limiting. - Minimum rollout channel: N/A. - Stable/default behavior changed: only when rate limiting is enabled and the configured TPM budget is exceeded. - Kill switch / disable path: disable rate limiting or increase `rate_limit_tokens_per_minute`. - Unsafe override required: No. - Qualification impact: deployments that configured a low TPM value will now receive the intended 429 responses. - Rollback path: revert this commit. ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [x] I did **not** edit `CHANGELOG.md` — it is generated by release-please from my Conventional Commit PR title (a CI guard enforces this) ## Screenshots (if applicable) N/A. ## Additional Notes Documentation is unchanged because the existing TPM configuration now behaves as documented. The warning above is the existing Starlette `TestClient` deprecation warning. |