Skip to main content

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:
  1. Recipe implementationqitos/recipes/<method_name>/__init__.py with AgentModule, Critic, and State
  2. Template assetstemplates/<method_name>/ with config, docs, and scaffold code
  3. Teststests/test_<method_name>.py
  4. CLI registration — Entry in _METHOD_TEMPLATES dict in qitos/cli.py

Directory Structure

Step 1: Define State

Create a dataclass tracking method-specific fields:
State is stored in EngineResult.state after execution and is accessible to the Critic at each step.

Step 2: Implement AgentModule

Subclass AgentModule with build_system_prompt() and reduce():

Key points

  • build_system_prompt() receives task (str) and state (dict). Use state to inject context like reflections, proposals, or task ledgers.
  • reduce() receives the current Decision and state, and returns a dict of state updates. The engine merges this into the running state.
  • Always handle state=None gracefully (first step).

Step 3: Implement Critic

Subclass Critic with evaluate() returning CriticResult:

CriticResult actions

ActionEffectWhen to use
continueEngine proceeds normallyMaking progress, no issues
stopEngine halts, returns resultTask complete or budget exhausted
retryEngine retries with instruction_patch injected into the promptError detected, need to change approach

Key fields

  • instruction_patch: String injected into the next prompt to guide the agent
  • state_patch: Dict merged into state to update method-specific fields
  • score: 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:
This makes it appear in qit list-templates output.

Step 6: Write Tests

Minimum test coverage:
Aim for at least 15 tests per template covering Agent, Critic, State, and edge cases.

Checklist

Before submitting a method template PR:
  • Agent subclasses AgentModule with build_system_prompt() and reduce()
  • Critic subclasses Critic with evaluate() returning CriticResult
  • State dataclass with method-specific fields
  • templates/<method_name>/ has __init__.py, agent.py, config.yaml, paper.md
  • Method name added to _METHOD_TEMPLATES in qitos/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>.py passes
  • Full test suite pytest tests/ -q shows no regressions

Existing Templates for Reference

TemplateFilePattern
Self-Refineqitos/recipes/self_refine/Generate → Critique → Refine
Reflexionqitos/recipes/reflexion/Act → Reflect → Retry
LATSqitos/recipes/lats/Monte Carlo Tree Search
MoAqitos/recipes/moa/Parallel Proposals + Aggregation
Magentic-Oneqitos/recipes/magentic_one/Orchestrator + Specialists