Choosing and Switching Models: Context, Cost, and Fallback Chains

9 min read Module 2 of 9 Topic 6 of 25

What you'll learn

  • Explain Hermes' context-length detection order and override it manually when needed
  • Match model choice to task type: quick lookups versus long-horizon reasoning
  • Configure hermes fallback so a provider outage does not stop your agent
  • Use hermes migrate to handle a retired or renamed model configuration
Building this at your company? For enterprise and company teams taking this to production: book a 30-minute session with our AI engineers for architecture guidance, code review, and a rollout plan for your use case.
Book a Team Session

Context Length: How Hermes Figures It Out

Hermes needs to know a model’s usable context window to manage memory retrieval, tool-result truncation, and conversation history correctly. It resolves this through a fixed priority order:

  1. model.context_length in config.yaml (highest priority, always wins if set)
  2. Custom provider per-model settings
  3. Persistent cache from a previous session
  4. The endpoint’s own /models API response
  5. The models.dev community registry
  6. A 128K fallback default
model:
  default: "qwen3.5:9b"
  context_length: 131072   # explicit override: skips detection entirely

Set this explicitly whenever you self-host (Lesson 5) and know the real figure, auto-detection against a local endpoint is the least reliable link in that chain, and a mismatch here is a common source of the “agent seems to forget things mid-task” symptom that looks like a memory bug (Module 4) but is actually a context-budgeting one.

Matching Model to Task

Not every request needs your strongest, most expensive model. A useful mental model:

Task shapeModel tierWhy
Quick factual lookup, short tool callFast, cheap tier (e.g. a mid-size open-weight model via OpenRouter)Latency and cost dominate; reasoning depth barely matters
Multi-step planning, ambiguous instructionsFrontier tier (Claude Sonnet 5, GPT-5.4, Gemini 3)Tool selection and plan quality benefit directly from stronger reasoning
Long-document analysis, large codebase refactorsFrontier tier + large context budgetBoth reasoning quality and context capacity matter
High-volume, low-stakes messaging bot repliesFast, cheap tierVolume makes per-token cost dominate the calculus

Switch mid-session with /model (Lesson 4) rather than restarting: start a task on a fast model, and escalate only the sub-question that actually needs deeper reasoning.

Building a Fallback Chain

Manual switching handles “I want a different model.” Fallback handles “my provider just went down and I’m not watching.” Configure it with:

hermes fallback

which walks you through an ordered list of backup providers. A resulting config looks like:

model:
  provider: "zai"
  default: "glm-5.2"

fallback:
  - provider: "anthropic"
    model: "claude-sonnet-5"
  - provider: "nous-portal"
    model: "gpt-5.4"

If the primary Z-AI GLM 5.2 connection errors out (rate limit, outage, expired key), Hermes automatically retries through Anthropic’s Claude Sonnet 5, then Nous Portal, in order, before surfacing a failure to you. Anthropic is a reasonable first fallback precisely because it’s a fully independent stack from Z-AI, a provider-level outage or rate limit on one has no bearing on the other, which is the entire point of a fallback chain: each hop should be able to fail for a different reason than the one before it. This matters most for the unattended deployments you’ll build in Module 5 (serverless backends) and Module 6 (messaging gateways), where nobody is at the keyboard to run /model when something breaks at 2am.

Handling Retired or Renamed Models

Providers periodically retire model names. Rather than manually hunting down every config reference, Hermes provides a migration helper:

hermes migrate xai   # example: migrate retired xAI model configs to current names

This walks your config.yaml and any custom_providers entries, flagging references to deprecated model identifiers and offering current replacements. Run it after a provider’s model deprecation announcement, before your fallback chain silently starts routing to a model that no longer exists.

Exercise: configure a two-provider fallback chain (your Module 4 primary plus one backup), then temporarily set an invalid API key for the primary provider in .env and confirm hermes doctor flags it, then confirm a chat request still succeeds by falling through to the backup. Restore the correct key when you’re done.

Knowledge Check

3 questions to test your understanding

1 You set `context_length: 131072` directly in config.yaml for a model, but Hermes had previously auto-detected 32768 tokens from a persistent cache. Which value wins?

2 Your primary provider (a direct Z-AI GLM 5.2 connection) starts returning 503s mid-task. What does a configured fallback chain do?

3 A task is 'summarize this one-page email and draft a two-sentence reply.' Another is 'read this 40-file codebase and refactor the authentication module.' What is the main model-selection lesson here?

Go further with expert guidance

Ready to build production AI?
Talk to our R&D team.

These courses give you the foundation. Our embedded AI teams take you from prototype to production in 30–90 days, with your team, your codebase, your goals. Book a free strategy call to see how we can accelerate your AI initiative.

30 minutes · No obligation · Expert AI engineers, not sales reps

AI Architecture Review

Audit your current stack and identify high-impact improvements

Project Review

Get expert feedback on your AI implementation and codebase

Team Mentoring

Upskill your engineers with hands-on AI coaching sessions

AI Strategy

Define your AI roadmap, prioritization, and implementation plan