Skip to content

LLM Layer

PantheraNestah edited this page Nov 5, 2026 · 1 revision

LLM Layer

Source: app/services/llm.py. Tests: tests/test_llm.py.

Application code never names a model. It asks for a tier and gets text back:

from app.services.llm import Tier, complete
text = complete(Tier.MEDIUM, system=SYSTEM, user=PROMPT, max_tokens=2000)

Each tier maps to a provider:model[:effort] setting from LLM_MODEL_COMPLEX|MEDIUM|EASY (Configuration). Clients are created lazily, so a provider whose key is missing only fails when a tier that uses it is first called.

Which task uses which tier

Task Tier max_tokens
/query with context medium 2000
/query small talk easy 2000
/learn/structured medium 6000
/learn/text complex 10000

max_tokens includes any reasoning tokens, so thinking models need headroom. A long /learn/text lesson at high effort can come close to the limit; raise TEXT_LESSON_MAX_TOKENS in generator.py if lessons are cut off (a hit max_tokens warning is logged).

Providers

Provider SDK Key Notes
anthropic native anthropic ANTHROPIC_API_KEY See below
deepseek openai SDK, base URL https://api.deepseek.com DEEPSEEK_API_KEY Model ID deepseek-flash
openai openai OPENAI_API_KEY Effort ignored; temperature honoured

Anthropic

  • Sonnet 5.5 and Haiku 5.5 do not accept temperature or top_p, so they are never sent.
  • Thinking is adaptive and always on. effort is sent as output_config.effort (low, medium, high, max). off is treated as low.
  • Prompt caching. The system prompt is sent with a cache_control breakpoint, because it is identical on every call of a task while the user message (retrieved text plus question) is unique. Prefixes under the model minimum (512 tokens for these models, per the provider documentation at the time of writing) are silently not cached. In measurements, the lesson prompts (about 960-980 tokens) were cached and the /query prompt (about 395 tokens) was not. Each call logs input, cache_write, cache_read and output tokens, which is the way to confirm caching. Disable with ANTHROPIC_PROMPT_CACHE=false.
  • Refusal fallback. For claude-sonnet-5-5 only, requests use the provider's server-side refusal-fallback beta (header server-side-fallback-2026-07-01). If the provider rejects that request, it is retried without it. Disable with ANTHROPIC_REFUSAL_FALLBACK=false. A refusal that is not recovered raises LLMError.
  • No prefill and no forced tool choice are used.

DeepSeek

  • effort=off disables thinking (thinking: {type: disabled}) and honours temperature.
  • Any other effort enables thinking and sends reasoning_effort. DeepSeek accepts low, high and max; it maps minimal to low and medium or xhigh to high.
  • temperature has no effect while thinking is on, so it is not sent.
  • Caching is automatic and needs no parameter. Usage lines show cache_hit, cache_miss and reasoning token counts.
  • deepseek-flash is the model ID in DeepSeek's documentation, which states it is served by DeepSeek-V4.1-Flash-1017. Third-party hosts use different names for that model; this project talks to DeepSeek directly.

JSON output

Both lesson endpoints ask for JSON. With json_mode=True, DeepSeek and OpenAI get response_format: json_object. Anthropic relies on the prompt. parse_json_object() strips code fences and, failing that, extracts the first { through the last }. DeepSeek can return blank content in JSON mode, so a blank result is retried once.

Errors

  • LLMError is raised for a refusal, an empty response, a malformed tier setting or a missing key.
  • Provider exceptions (authentication, rate limit, network) propagate unchanged.
  • The generator catches both and returns a fallback body (API Reference).

Adding a provider

  1. Add its name to _PROVIDERS.
  2. Write a _call_<provider> function that returns text.
  3. Dispatch to it in complete().
  4. Add tests with a fake client, following tests/test_llm.py.

Provider model names and beta headers change. Treat the values in this page and in the code as correct at the time of writing and confirm them against the provider's documentation.

Clone this wiki locally