Skip to content

fix(api-keys): map enforced service_tier auto/default to outbound omission - #611

Merged
Soju06 merged 2 commits into
Soju06:mainfrom
ozpool:fix/enforced-service-tier-default-546
May 18, 2026
Merged

Soju06 merged 2 commits into
Soju06:mainfrom
ozpool:fix/enforced-service-tier-default-546

Conversation

@ozpool

@ozpool ozpool commented May 14, 2026

Copy link
Copy Markdown
Contributor

Summary

Resolves #546.

Setting an API key's enforced_service_tier to default or auto previously broke every request through that key: the request failed upstream with invalid_request_error Unsupported service_tier: default (or ... auto). codex-lb accepts both values at the API-key surface as conventions for "let upstream pick", but the wire-level equivalent of "let upstream pick" is the absence of the service_tier field, not the literal string.

What changes

  • In apply_api_key_enforcement (app/modules/proxy/request_policy.py), when enforced_service_tier resolves to auto or default, set payload.service_tier = None instead of the literal value. Real upstream tiers (priority, flex) keep propagating as the literal value unchanged.
  • Extend the existing api_key_service_tier_enforced log line with outbound_service_tier= so operators can confirm enforcement reached upstream as expected.
  • OpenSpec change folder under openspec/changes/fix-enforced-service-tier-default/ adds the contract under api-keys.

Test plan

  • pytest tests/unit/test_proxy_utils.py (228 tests pass; 3 new assertions: default/auto omission paths, plus a flex propagation regression guard)
  • Existing test_apply_api_key_enforcement_overrides_service_tier_aliases_to_priority continues to pass — the alias-to-priority rewrite path is unchanged.
  • ruff check, ty check clean on the touched files.

…ssion

Setting an API key's enforced_service_tier to default or auto previously
forwarded the literal string to upstream, which the ChatGPT/Codex
backend rejects with "Unsupported service_tier: default" /
"... auto". codex-lb accepts both values at the API-key surface as
conventions for "let upstream pick", but the wire-level equivalent
is the absence of the service_tier field, not the literal.

In apply_api_key_enforcement, when enforced_service_tier resolves to
auto or default, set payload.service_tier = None instead of the
literal so enforcement actually reaches upstream. Real upstream tiers
(priority, flex) keep propagating as the literal value unchanged.

The existing api_key_service_tier_enforced log line is extended with
outbound_service_tier= so operators can confirm what was actually sent
upstream when the API-key enforcement differs from the wire value.

Unit coverage in tests/unit/test_proxy_utils.py:
- default and auto omission paths
- priority/flex still propagate as literals
- existing aliases-to-priority path unchanged

Resolves Soju06#546.
@Soju06

Soju06 commented May 15, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a8fce5b772

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

else:
effective_service_tier = api_key.enforced_service_tier
setattr(payload, "service_tier", effective_service_tier)
if requested_service_tier != api_key.enforced_service_tier:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Compare against effective tier before logging enforcement

The new auto/default -> None mapping changes the outbound value, but the log guard still compares requested_service_tier to api_key.enforced_service_tier. For keys enforced to auto or default, requests that already omit service_tier (None) now trigger api_key_service_tier_enforced on every call even though the outbound tier is unchanged (None), which can create high-volume false-positive enforcement logs in normal traffic and make this signal noisy for operators.

Useful? React with 👍 / 👎.

@Soju06

Soju06 commented May 15, 2026

Copy link
Copy Markdown
Owner

Thanks @ozpool -- the framing is exactly right (the wire-level equivalent of "let upstream pick" is the absence of the field, not the literal string), and the OpenSpec change folder is in place. Two items to address before merging.

P2 from codex review -- log noise after the auto/default -> None remap

The new mapping changes the outbound value but the log guard still compares against the original enforced literal:

# app/modules/proxy/request_policy.py:~99
if requested_service_tier != api_key.enforced_service_tier:
    logger.info(
        "api_key_service_tier_enforced request_id=%s key_id=%s "
        "requested_service_tier=%s enforced_service_tier=%s "
        "outbound_service_tier=%s",
        ...
    )

For an API key enforced to auto or default, requests that already omit service_tier (requested_service_tier=None) now log api_key_service_tier_enforced on every call -- because None != "auto". But the outbound tier is None either way, so the enforcement didn't actually change anything. Operators will see this as a high-volume false-positive enforcement signal.

Suggested guard: compare against the effective outbound tier instead of the literal:

if requested_service_tier != effective_service_tier:
    logger.info(
        "api_key_service_tier_enforced request_id=%s key_id=%s "
        "requested_service_tier=%s enforced_service_tier=%s "
        "outbound_service_tier=%s",
        get_request_id(),
        api_key.id,
        requested_service_tier,
        api_key.enforced_service_tier,
        effective_service_tier,
    )

Now the log only fires when the outbound tier actually differs from what the client sent, which is the signal operators care about. The enforced_service_tier literal is still in the log line for diagnostic context.

Regression to add against the existing tests/unit/test_proxy_utils.py test block:

def test_enforced_auto_does_not_log_when_request_already_omits_service_tier(caplog):
    # request omits service_tier (the common case), key enforces "auto"
    # outbound is None either way -> no api_key_service_tier_enforced log
    with caplog.at_level(logging.INFO):
        apply_api_key_enforcement(make_payload(service_tier=None), make_api_key(enforced_service_tier="auto"))
    assert not any("api_key_service_tier_enforced" in r.getMessage() for r in caplog.records)

And the symmetric one confirming the log still fires when the outbound actually differs (client sent priority, key enforces default -> outbound None).

CI: Lint (ruff) fail on tests/unit/test_proxy_utils.py

The Lint job is red on this head:

Would reformat: tests/unit/test_proxy_utils.py
1 file would be reformatted, 434 files already formatted

Running uv run ruff format tests/unit/test_proxy_utils.py locally should land the formatter delta. Once that's pushed the rest of the merge gates pass cleanly.

Not blocking otherwise

  • The mapping decision (auto/default -> outbound None, priority/flex literal-through) is correct.
  • OpenSpec change folder is in place.
  • outbound_service_tier= in the log line is exactly the operator-visibility piece that was missing -- keep it.

After the P2 fix and ruff format land I'll re-run @codex review and queue for merge.

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create an environment for this repo.

@Komzpa Komzpa added the 🤖 codex: needs work [@codex review] raised an issue label May 16, 2026
@Komzpa Komzpa removed the 🤖 codex: needs work [@codex review] raised an issue label May 17, 2026
@Komzpa

Komzpa commented May 17, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Komzpa Komzpa added the 🤖 codex: ok [@codex review] says no issues found. label May 17, 2026
@Soju06
Soju06 merged commit 9a64e23 into Soju06:main May 18, 2026
18 checks passed
Soju06 added a commit that referenced this pull request May 19, 2026
## [1.18.2](v1.18.1...v1.18.2) (2026-05-19)


### Bug Fixes

* **api-keys:** map enforced service_tier auto/default to outbound omission ([#611](#611)) ([9a64e23](9a64e23))
* **api-keys:** size usage reservations from request budget ([#706](#706)) ([ea72eea](ea72eea))
* **dashboard:** derive quota status from usage windows ([#686](#686)) ([c463def](c463def))
* **dashboard:** make weekly credit pace backend-driven ([#692](#692)) ([313783c](313783c))
* **db:** recover stale reservations and serialize sqlite writers ([#667](#667)) ([6635628](6635628))
* **model-refresh:** refresh HTTP client on transport errors ([#547](#547)) ([2087df0](2087df0))
* **models:** report full context in v1 metadata ([#681](#681)) ([c3c977d](c3c977d))
* **proxy:** cap selector "Try again in" hint at 300s ([#690](#690)) ([cf09bd6](cf09bd6)), closes [#676](#676) [#678](#678)
* **proxy:** eliminate /v1 responses cancel/retry stream contamination ([#696](#696)) ([c9da497](c9da497))
* **proxy:** harden long Codex websocket turns ([#674](#674)) ([12bec0f](12bec0f))
* **proxy:** include sticky thread in budget-pressure guard ([#514](#514)) ([1e2c6d6](1e2c6d6))
* **proxy:** keep idle timeout classification after scheduler jitter ([#693](#693)) ([17e8abc](17e8abc))
* **proxy:** mask partial previous-response stream errors ([#689](#689)) ([a4a138c](a4a138c))
* **proxy:** persist request logs outside cancellation ([#688](#688)) ([6e8fa56](6e8fa56))
* **proxy:** repair interrupted Codex response continuity ([#698](#698)) ([682447f](682447f))
* **proxy:** trim durable full-resend continuations ([#687](#687)) ([dfc38c0](dfc38c0))
* **security:** remediate Docker code scanning alerts ([#699](#699)) ([de24c2e](de24c2e))


### Documentation

* add AnobleSCM as a contributor for code, and test ([#695](#695)) ([718931e](718931e))
* add jgorostegui as contributor ([#697](#697)) ([d9d5a6e](d9d5a6e))
* add linusmixson as contributor ([#705](#705)) ([f3ad60a](f3ad60a))

---
This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
vitalNohj pushed a commit to vitalNohj/codex-lb that referenced this pull request Jul 17, 2026
…ssion (Soju06#611)

* fix(api-keys): map enforced service_tier auto/default to outbound omission

Setting an API key's enforced_service_tier to default or auto previously
forwarded the literal string to upstream, which the ChatGPT/Codex
backend rejects with "Unsupported service_tier: default" /
"... auto". codex-lb accepts both values at the API-key surface as
conventions for "let upstream pick", but the wire-level equivalent
is the absence of the service_tier field, not the literal.

In apply_api_key_enforcement, when enforced_service_tier resolves to
auto or default, set payload.service_tier = None instead of the
literal so enforcement actually reaches upstream. Real upstream tiers
(priority, flex) keep propagating as the literal value unchanged.

The existing api_key_service_tier_enforced log line is extended with
outbound_service_tier= so operators can confirm what was actually sent
upstream when the API-key enforcement differs from the wire value.

Unit coverage in tests/unit/test_proxy_utils.py:
- default and auto omission paths
- priority/flex still propagate as literals
- existing aliases-to-priority path unchanged

Resolves Soju06#546.

* style(tests): format proxy utils

---------

Co-authored-by: Darafei Praliaskouski <[email protected]>
vitalNohj pushed a commit to vitalNohj/codex-lb that referenced this pull request Jul 17, 2026
## [1.18.2](Soju06/codex-lb@v1.18.1...v1.18.2) (2026-05-19)


### Bug Fixes

* **api-keys:** map enforced service_tier auto/default to outbound omission ([Soju06#611](Soju06#611)) ([ce0d1ea](Soju06@ce0d1ea))
* **api-keys:** size usage reservations from request budget ([Soju06#706](Soju06#706)) ([f3b1689](Soju06@f3b1689))
* **dashboard:** derive quota status from usage windows ([Soju06#686](Soju06#686)) ([acb4133](Soju06@acb4133))
* **dashboard:** make weekly credit pace backend-driven ([Soju06#692](Soju06#692)) ([383ccd4](Soju06@383ccd4))
* **db:** recover stale reservations and serialize sqlite writers ([Soju06#667](Soju06#667)) ([79dced5](Soju06@79dced5))
* **model-refresh:** refresh HTTP client on transport errors ([Soju06#547](Soju06#547)) ([f0b38a0](Soju06@f0b38a0))
* **models:** report full context in v1 metadata ([Soju06#681](Soju06#681)) ([82bd538](Soju06@82bd538))
* **proxy:** cap selector "Try again in" hint at 300s ([Soju06#690](Soju06#690)) ([1242bc1](Soju06@1242bc1)), closes [Soju06#676](Soju06#676) [Soju06#678](Soju06#678)
* **proxy:** eliminate /v1 responses cancel/retry stream contamination ([Soju06#696](Soju06#696)) ([d7f3cea](Soju06@d7f3cea))
* **proxy:** harden long Codex websocket turns ([Soju06#674](Soju06#674)) ([0828e63](Soju06@0828e63))
* **proxy:** include sticky thread in budget-pressure guard ([Soju06#514](Soju06#514)) ([e9d5b84](Soju06@e9d5b84))
* **proxy:** keep idle timeout classification after scheduler jitter ([Soju06#693](Soju06#693)) ([0860872](Soju06@0860872))
* **proxy:** mask partial previous-response stream errors ([Soju06#689](Soju06#689)) ([4563488](Soju06@4563488))
* **proxy:** persist request logs outside cancellation ([Soju06#688](Soju06#688)) ([684b1dc](Soju06@684b1dc))
* **proxy:** repair interrupted Codex response continuity ([Soju06#698](Soju06#698)) ([0e8232a](Soju06@0e8232a))
* **proxy:** trim durable full-resend continuations ([Soju06#687](Soju06#687)) ([e017bfc](Soju06@e017bfc))
* **security:** remediate Docker code scanning alerts ([Soju06#699](Soju06#699)) ([1b012f6](Soju06@1b012f6))


### Documentation

* add AnobleSCM as a contributor for code, and test ([Soju06#695](Soju06#695)) ([e95cfca](Soju06@e95cfca))
* add jgorostegui as contributor ([Soju06#697](Soju06#697)) ([4974c52](Soju06@4974c52))
* add linusmixson as contributor ([Soju06#705](Soju06#705)) ([ed41ae0](Soju06@ed41ae0))

---
This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤖 codex: ok [@codex review] says no issues found.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enforcing default service tier does not work

3 participants