Skip to content

Add model tiers and try fallback models on retry - #5

Open
m-messer wants to merge 3 commits into
mainfrom
model-tiers
Open

m-messer wants to merge 3 commits into
mainfrom
model-tiers

Conversation

@m-messer

@m-messer m-messer commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Summary

configuration.params.model can now be a tier name instead of a specific OpenRouter ID, so tasks keep working when a model is retired. The tier-to-model mapping lives in one dict in evaluation_function/models.py. Replacing a retired model means editing that dict and redeploying.

Tier Primary model Built-in fallback
small openai/gpt-6-luna anthropic/claude-haiku-5.5
medium (new default) google/gemini-3.8-flash openai/gpt-6.1-terra
large anthropic/claude-sonnet-5.5 openai/gpt-6.2-sol
reasoning openai/gpt-6.2-sol (sends reasoning: {"effort": "low"}) anthropic/claude-sonnet-5.5

Explicit model IDs behave as before. If model is omitted, the medium tier is used instead of openai/gpt-4o-mini.

Fallback models are now actually tried

Fallbacks used to go in OpenRouter's models field. OpenRouter only falls back on errors it sees itself, never on our own 8s per-attempt timeout, so a slow primary model was retried but never replaced. The retry loop in _request_json now handles fallbacks itself, and models is no longer sent to OpenRouter:

  • Timeouts and provider errors: each retry after a timeout, connection error, 408, 429 or 5xx uses the next model in the list. That's the tier's built-in fallback, then any fallback_models. When the list runs out, the last model is reused.
  • Rejected model IDs: if there is a next model, a 400 or 404 also moves on to it. OpenRouter answers a retired or unknown model ID with a 400 is not a valid model ID, not a 404, as staging showed. A content-filter 400 still counts as failing moderation and doesn't fall back.
  • Unparseable output: if there is a next model, a reply that isn't valid JSON also moves on to it.
  • 401, 402 and 403 errors still fail without a retry.
  • Each step gets at least two attempts and can try every model in the list, still within the 22s deadline.
  • A 429's Retry-After wait is skipped when the next attempt uses a different model.
  • Retry logs name both models.

Claude JSON parsing fix

Every Claude model was failing on staging with failed to parse ... result as JSON, including claude-haiku-4.5 from the README examples. The usual cause is Claude wrapping its JSON in a Markdown code fence even when response_format asks for plain JSON. JSON wrapped in a code fence or surrounded by text is now accepted. A JSON value that isn't an object, such as a bare true, counts as a parse failure instead of raising an error. Logs record only whether the output was fenced, never its content.

I didn't switch to structured outputs because Bedrock's ZDR endpoints for the Claude 5.x models don't support them.

Behaviour changes to note

  • Tier names must be exact and lowercase. Something like "Medium" is treated as an OpenRouter ID and fails as an invalid model.
  • fallback_models takes OpenRouter IDs only. Tier names in it are not expanded.

Test plan

  • poetry run pytest: 77 passed, 1 skipped (the opt-in live check)
  • CHECK_MODEL_TIERS_ZDR=1 poetry run pytest evaluation_function/models_test.py -k zdr: all 8 tier models have a ZDR endpoint that supports response_format
  • Staging (LLM_Caller-staging, deployed from this branch at 7e93103): 13/13 cases pass
Case Result
Default (no model), small, medium, large, reasoning ✅ Correct verdict with feedback, 3.8–12.9s
medium and large with a wrong answer ✅ Marked incorrect
claude-sonnet-5.5 and claude-haiku-5.5 alone (no fallback) ✅ Previously always failed to parse
Invalid model ID + fallback_models ✅ Fell back, 3.3s
Explicit openai/gpt-4o-mini ✅ Unchanged
Invalid model ID with no fallback, and "Medium" ✅ Fail with a 500, as expected

The staging logs also show the timeout fallback working on a real request. Correctness timed out on gemini-3.8-flash after 8s, and the retry on gpt-6.1-terra answered in 1.95s. reasoning took 3.8s, well under the 8s per-attempt cap.

🤖 Generated with Claude Code

`model` can now be a tier name (small, medium, large, reasoning) instead of
an OpenRouter ID, so tasks keep working when a model is retired: the
tier-to-model mapping lives in one dict in evaluation_function/models.py.
Each tier has a primary model and a built-in fallback from another
provider; the reasoning tier also sends `reasoning: {"effort": "low"}`.
Omitting `model` now uses the medium tier instead of openai/gpt-4o-mini.

Fallbacks were previously passed to OpenRouter's `models` field, which only
acts on errors OpenRouter sees, never on our 8s client timeout, so a slow
primary was retried rather than replaced. The retry loop now moves to the
next model on each retry (tier fallback, then any `fallback_models`), and a
404 also moves on, so a retired model falls back. Each step gets at least
two attempts and can try every model while the deadline allows; Retry-After
is skipped when switching model. `models` is no longer sent to OpenRouter.

An opt-in test (CHECK_MODEL_TIERS_ZDR=1) checks every tier model against
OpenRouter's live ZDR endpoint list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Staging showed two gaps in the model fallback:

- OpenRouter rejects an unknown or retired model ID with a 400 "is not a
  valid model ID", not a 404, so the fallback never triggered. A 400 or 404
  (other than a content filter block) now moves on to the next model.
- Every Claude model's output failed to parse as JSON, and a parse failure
  returned the "please try again" feedback without trying the fallback.
  JSON wrapped in a code fence or surrounded by text is now accepted, output
  that still can't be parsed moves on to the next model, and a non-object
  JSON value counts as a parse failure instead of raising. Logs record only
  whether the output was fenced, never its content.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@peterbjohnson

Copy link
Copy Markdown
Member

Thanks for the suggested changes.

I've found significant differences between Gemini and GPT, when prompting feedback.

Do you think it would be reasonable - i.e. not too complicated - to have a tier per major model supplier? E.g. an extensible list starting with Google, OpenAI, Anthropic, DeepSeek?

Separately, where is the ideal place to manage model tiers? Eval Function or tenant?

A task can set `provider` (openai, google, anthropic, deepseek) next to a
tier name to use that provider's version of the tier. Provider tiers fall
back only to models from the same provider, so feedback style stays
consistent. An unknown provider is a configuration error; provider is
ignored for explicit model IDs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@m-messer

m-messer commented Nov 26, 2026 •

Copy link
Copy Markdown
Member Author

Thanks for the comment. I've switched to having a provider parameter and different models per provider instead of different ones on each.

I think default parameters could be part of the tenant; as we upgrade the params in the future, this could be added then.

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.

2 participants