Tree-of-Thought Example Walkthrough (Branch + Search Selection)¶
What this agent is¶
examples/patterns/tot.py implements a Tree-of-Thought-style loop:
- the model proposes multiple candidate next actions (“thoughts”)
- the agent converts them into
Decision.branch(candidates=[...]) - the Engine selects a candidate (via
Searchor a default selector)
This is the minimal QitOS pattern that demonstrates branching semantics.
Core idea¶
ToT is not “one prompt”. It is a search policy:
- expand candidates (multiple thoughts)
- score them
- prune
- select one to execute
In QitOS terms:
- candidate generation lives in
AgentModule.decide(...) - candidate selection lives in
EngineviaSearch(if provided)
Method-by-method design¶
State: evidence accumulation matters more than scratchpad¶
Design principle:
- In ToT, you are searching for evidence, not just “taking actions”.
What the example does:
- stores
evidencesnippets from EPUB operations - uses evidence as the context for the next thought expansion
decide: return Decision.branch(...)¶
Design principle:
- Branching must be explicit and typed, otherwise you cannot plug search.
What the example does:
- step 0 bootstraps with a deterministic action (
epub.list_chapters) - later steps call LLM to propose a JSON structure:
- list of thoughts, each has
idea,score, and tool action - converts each thought into
Decision.act(...)and returnsDecision.branch(...)
Engine wiring: attach a Search implementation¶
The example passes:
Design principle:
- Search policy should be swappable without rewriting the agent.
What DynamicTreeSearch does (conceptually):
- keeps top-K candidates
- uses per-candidate scores (from decision meta) to choose one
reduce: evidence is the invariant¶
Design principle:
- ToT “progress” is evidence quality, not cursor increments.
What the example does:
- extracts
hits[].snippetorcontentfrom EPUB tool results - appends snippets to evidence, keeping it bounded
How to evolve this into a real research ToT¶
- Separate expand vs score:
- let model propose candidates, but have a verifier score them
- Add backtracking:
- if pruning removes everything, call
Search.backtrack(state) - Track a tree explicitly:
- store nodes/edges in state and expose them in trace