Repository navigation
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.
| 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).
| 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 |
- Sonnet 5.5 and Haiku 5.5 do not accept
temperatureortop_p, so they are never sent. - Thinking is adaptive and always on.
effortis sent asoutput_config.effort(low,medium,high,max).offis treated aslow. -
Prompt caching. The system prompt is sent with a
cache_controlbreakpoint, 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/queryprompt (about 395 tokens) was not. Each call logsinput,cache_write,cache_readandoutputtokens, which is the way to confirm caching. Disable withANTHROPIC_PROMPT_CACHE=false. -
Refusal fallback. For
claude-sonnet-5-5only, requests use the provider's server-side refusal-fallback beta (headerserver-side-fallback-2026-07-01). If the provider rejects that request, it is retried without it. Disable withANTHROPIC_REFUSAL_FALLBACK=false. A refusal that is not recovered raisesLLMError. - No prefill and no forced tool choice are used.
-
effort=offdisables thinking (thinking: {type: disabled}) and honourstemperature. - Any other effort enables thinking and sends
reasoning_effort. DeepSeek acceptslow,highandmax; it mapsminimaltolowandmediumorxhightohigh. -
temperaturehas no effect while thinking is on, so it is not sent. - Caching is automatic and needs no parameter. Usage lines show
cache_hit,cache_missandreasoningtoken counts. -
deepseek-flashis 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.
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.
-
LLMErroris 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).
- Add its name to
_PROVIDERS. - Write a
_call_<provider>function that returns text. - Dispatch to it in
complete(). - 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.
Documentation last verified against the code on 2026-11-05. Evaluation figures are historical, see Evaluation. Educational tool, not medical advice.
Use it
How it works
- Architecture
- Retrieval Pipeline
- LLM Layer
- Prompts and Lessons
- Grounding Verification
- Knowledge Base
- Data and Logging
Quality
Develop