[Feature Request]: Configurable source limits for books and Mastery Path
Feature Request Description
Make the source limits for books and Mastery Path configurable. A long lesson PDF or a full notebook is cut well before it reaches the model:
learning/topic_materials.py:MAX_CHAPTER_CHARS = 24_000,MAX_BOOK_CHARS = 240_000,MAX_NOTEBOOK_CHARS = 120_000,MAX_TOTAL_CHARS = 600_000,MAX_CHAPTERS_PER_BOOK = 40learning/topic_generation.py:_MAX_SOURCES = 16,_MAX_SOURCE_EXCERPT = 4_000,_MAX_SOURCE_TOTAL = 24_000,_MAX_KB_DOCUMENTS = 60book/inputs.py: 32 source refs, 80,000 chars of source textbook/agents/source_explorer.py,spine_synthesizer.py,blocks/section.py: chunk clips at 280-1,800 chars and caps of 18-96 chunks
Related Module
book, learning.
Use Case
With long-context models (1M tokens), the defaults throw most of a course away: a 300-page lecture-notes PDF reaches a book as a few dozen 320-character snippets. We raised them locally so whole courses reach the model; the trade-off is cost, which is why it should be the deployer's choice rather than one fixed number.
Additional Context
Proposal: one settings group (or env vars) with today's values as defaults, so nothing changes unless a deployer opts in. We patch about 30 of these constants locally and can turn that into a PR once the shape is agreed.
+1 on "defaults unchanged, deployer opts in" — that constraint should be non-negotiable in whatever shape lands. Here is a concrete shape proposal so the PR has something concrete to agree on.
Where limits live: a dedicated env namespace, not the existing Settings class.
The current env plumbing (deeptutor/config/settings.py) is pydantic-settings with env_prefix="LLM_" + __ nesting. Source limits are not LLM settings — nesting them under LLM_LIMITS__* would misname them at the deployment layer, and pydantic-settings pins one env_prefix per class. So rather than overload the prefix, add a second BaseSettings in the same module:
class TopicMaterialsLimits(BaseModel):
max_chapter_chars: int = Field(default=24_000, ge=1)
max_book_chars: int = Field(default=240_000, ge=1)
max_notebook_chars: int = Field(default=120_000, ge=1)
max_total_chars: int = Field(default=600_000, ge=1)
max_chapters_per_book: int = Field(default=40, ge=1)
class TopicGenerationLimits(BaseModel):
max_sources: int = Field(default=16, ge=1)
max_source_excerpt: int = Field(default=4_000, ge=1)
max_source_total: int = Field(default=24_000, ge=1)
max_kb_documents: int = Field(default=60, ge=1)
class LimitsSettings(BaseSettings):
topic_materials: TopicMaterialsLimits = Field(default_factory=TopicMaterialsLimits)
topic_generation: TopicGenerationLimits = Field(default_factory=TopicGenerationLimits)
# book_inputs / book_agents groups likewise
model_config = SettingsConfigDict(env_prefix="DEEPTUTOR_", env_nested_delimiter="__")
limits = LimitsSettings()
Env surface: DEEPTUTOR_LIMITS__TOPIC_GENERATION__MAX_SOURCES=64, etc. Unset ⇒ today's values byte-for-byte, so the change is invisible unless a deployer opts in — matching your use case ("cost is the deployer's choice"). Today's constants move into the field defaults so the numbers stay documented in one place.
Wiring pattern: each call site replaces the module-level constant read with the accessor (limits.topic_generation.max_sources instead of _MAX_SOURCES). One PR is enough if the call sites are ~30 mechanical replacements; the important part is that no constant is read directly anymore, so the next limit can't silently fork from settings.
Two scoping calls worth agreeing before the PR:
- Env vs admin settings. There are two config systems in tree: this env-based one (deploy-time, used by
agents/base_agent.py,services/llm/factory.py) andload_system_settings()(DB-backed, admin UI, used fordisable_ssl_verify,capability_routing_enabled). I'd put source limits in the env group: they're deployment-level resource/cost decisions, not per-workspace preferences, and they must be knowable before the first turn. Admin-UI visibility can be added later on top without changing the shape. - Which of the ~30 constants get exposed. The volume caps (chars, sources, chunks) are clearly deployer-tunable. But some of the 280–1,800-char clips in
book/agents/*are prompt-shaping, not just volume — raising a snippet clip changes how the agent reads, not just how much. I'd expose the volume caps and leave prompt-shaping clips internal until someone actually needs them tuned; happy to be overruled if your local patches found real wins there.
Validation: ge=1 constraints keep nonsense values (0/negative) out at parse time; a cross-field check (excerpt ≤ total) is cheap and worth having.
Tests: a snapshot test asserting the effective values equal today's constants with no env set (the "defaults unchanged" guarantee, enforced), plus one integration test overriding e.g. MAX_SOURCES through the env var to prove the wire reaches the call site.
If this shape works for you, the natural PR order is: settings group + accessor + one wired module (topic_generation.py) to establish the pattern, then the remaining call sites. That keeps the diff reviewable while locking the pattern in first.
Thanks, this shape works for us. On the two scoping calls:
- Env, not admin settings. Agreed.
DEEPTUTOR_also matches the env vars already read directly in the tree (runtime/mode.py,services/session/context_builder.py, …), soDEEPTUTOR_LIMITS__…fits. - Volume caps first, prompt-shaping clips internal. Agreed. Locally we raised the evidence-chunk clips (280–320 → 6,000 chars) together with the volume caps and never A/B-tested them separately, so we have no evidence the clip on its own is a win. One exception worth looking at: in
source_explorer.pya notebook record becomes a single chunk clipped to 1,200 chars, so the rest of the record is dropped rather than reshaped. Locally we split it into 3,000-char chunks instead. That is closer to a volume bug than a tuning knob, and could be its own small fix.
Validation and tests as you describe (defaults snapshot with no env set, plus one env override reaching topic_generation.py) sound right, as does the PR order. Once a maintainer is fine with the shape, I'll open the first PR: settings group, accessor, topic_generation.py wired.
Great — shape agreed on both scoping calls, and the DEEPTUTOR_ consistency point with runtime/mode.py / services/session/context_builder.py is a good catch that settles the prefix question for good.
On the source_explorer.py finding: verified in code, and I agree it's a volume bug rather than a tuning knob. text=_clip(str(text), 1200) at source_explorer.py:469 (with the same 1200-char pattern at lines 621 and 641) truncates a notebook record to one chunk and the remainder is simply dropped — nothing reshapes or re-chunks it. Your local split into 3,000-char chunks is the right instinct for it; as its own small fix it also stays testable independently of the settings group (which only exposes limits — a dropped-record bug is a correctness issue regardless of what the limits are).
Suggested order: your settings-group PR first (it locks the wiring pattern), the source_explorer chunk-split as a second small PR — happy to review either, and the tests-as-guarantee shape (defaults snapshot + one env override) is exactly what I'd check first on the first PR.
Thanks for the discussion in #2604. I have a related question about a source-grounded Course Factory workflow I'm developing locally.
The current prototype selects up to 30 PDF pages using deterministic even-stride sampling, extracts those pages directly with PyMuPDF, and generates an outline and lessons without creating embeddings or a vector index.
For long documents, I want the UI and provenance manifest to make it explicit that only selected pages were inspected, and citations must only reference those inspected pages.
Would you recommend treating this page budget as part of the same deployment-level DEEPTUTOR_ source-limit configuration discussed here, or keeping a separate Course Factory-specific limit?
My preference is to preserve safe existing defaults, make the actual coverage transparent, and allow deployment-level adjustment without introducing another provider or indexing system.