Writing a Method Template
A method template in QitOS is an Agent + Critic pair implementing a well-known agentic reasoning pattern. This guide walks through creating one from scratch.What Makes a Method Template
Every method template consists of:- Recipe implementation —
qitos/recipes/<method_name>/__init__.pywith AgentModule, Critic, and State - Template assets —
templates/<method_name>/with config, docs, and scaffold code - Tests —
tests/test_<method_name>.py - CLI registration — Entry in
_METHOD_TEMPLATESdict inqitos/cli.py
Directory Structure
Step 1: Define State
Create a dataclass tracking method-specific fields:EngineResult.state after execution and is accessible to the Critic at each step.
Step 2: Implement AgentModule
SubclassAgentModule with build_system_prompt() and reduce():
Key points
build_system_prompt()receivestask(str) andstate(dict). Use state to inject context like reflections, proposals, or task ledgers.reduce()receives the currentDecisionandstate, and returns a dict of state updates. The engine merges this into the running state.- Always handle
state=Nonegracefully (first step).
Step 3: Implement Critic
SubclassCritic with evaluate() returning CriticResult:
CriticResult actions
| Action | Effect | When to use |
|---|---|---|
continue | Engine proceeds normally | Making progress, no issues |
stop | Engine halts, returns result | Task complete or budget exhausted |
retry | Engine retries with instruction_patch injected into the prompt | Error detected, need to change approach |
Key fields
instruction_patch: String injected into the next prompt to guide the agentstate_patch: Dict merged into state to update method-specific fieldsscore: Float (0-1) indicating quality; used by qita for visualization
Step 4: Create Template Assets
agent.py
config.yaml
paper.md
Step 5: Register in CLI
Add to_METHOD_TEMPLATES in qitos/cli.py:
qit list-templates output.
Step 6: Write Tests
Minimum test coverage:Checklist
Before submitting a method template PR:- Agent subclasses
AgentModulewithbuild_system_prompt()andreduce() - Critic subclasses
Criticwithevaluate()returningCriticResult - State dataclass with method-specific fields
-
templates/<method_name>/has__init__.py,agent.py,config.yaml,paper.md - Method name added to
_METHOD_TEMPLATESinqitos/cli.py - Tests cover Agent, Critic, and State with edge cases (minimum 15 tests)
- paper.md explains mapping from paper to QitOS and key differences
-
pytest tests/test_<method_name>.pypasses - Full test suite
pytest tests/ -qshows no regressions
Existing Templates for Reference
| Template | File | Pattern |
|---|---|---|
| Self-Refine | qitos/recipes/self_refine/ | Generate → Critique → Refine |
| Reflexion | qitos/recipes/reflexion/ | Act → Reflect → Retry |
| LATS | qitos/recipes/lats/ | Monte Carlo Tree Search |
| MoA | qitos/recipes/moa/ | Parallel Proposals + Aggregation |
| Magentic-One | qitos/recipes/magentic_one/ | Orchestrator + Specialists |
