reduce and check_stop, giving you a structured hook to inspect the agent’s latest decision and its results, assign a quality score, and — when necessary — force a retry or halt execution.
This tutorial covers the full critic API: the CriticResult contract, built-in critics, the @critic decorator, instruction and state patches, and composing multiple critics together.
Step 1: The CriticResult Contract
Every critic returns aCriticResult — a dataclass that tells the Engine what to do next.
The three actions control the loop:
"continue"— proceed to the next step as normal."stop"— halt the Engine immediately. The state’s stop reason is set toStopReason.CRITIC_STOP."retry"— discard the current step and re-rundecide. Anyinstruction_patchorstate_patchis applied before the retry.
Step 2: Using Built-in Critics
QitOS ships with ready-made critics inqitos.kit.critic.
PassThroughCritic
The simplest critic — always continues with a perfect score.SelfReflectionCritic
Inspects tool results for errors and retries automatically up to a configurable limit.- If any tool result contains an
{"error": ...}key and retries have not been exhausted, it returnsaction="retry"withscore=0.2. - If errors persist beyond
max_retries, it returnsaction="stop"withscore=0.0. - If no errors are found, it returns
action="continue"withscore=1.0.
ReActSelfReflectionCritic
A richer variant designed for ReAct agents. On error, it builds a structured reflection note describing the failed action and the observed error, then appends it to the state’s metadata so the LLM can learn from the failure on the next retry.Functional Equivalents
Each built-in critic also ships as a decorated function:Step 3: Writing a Custom Critic with the @critic Decorator
The@critic decorator converts any plain function into a Critic instance. The function receives (state, decision, results) and can return a quick shorthand or a full CriticResult.
A bare decorator (no arguments):
score parameter is used as the default. In the example above, a "continue" return gets score=0.8 instead of the usual 1.0.
Wire the critic into the Engine:
Step 4: Critic with Instruction and State Patches
When a critic returnsaction="retry", it can guide the next iteration in two ways:
instruction_patch— a string appended to the agent’s system prompt, giving the LLM additional guidance.state_patch— a dictionary whose keys and values are merged into the agent’s state object viasetattr.
decide again, so the LLM and the state are already updated when the retry begins.
state_patch to inject tracking data:
Step 5: Combining Critics
The Engine accepts a list of critics. They are evaluated in order, and the first non-continue result wins. This lets you stack critics from strictest to most permissive.- If
safety_gatereturns"stop", the Engine halts immediately. Later critics are never called. - If
safety_gatereturns"continue"butformat_checkreturns"retry", the Engine retries with the instruction patch. - If both return
"continue", theSelfReflectionCriticgets a chance to catch tool errors.
Hooks Lifecycle
Understand the full Engine event lifecycle that critics participate in.
Agent Module
Deep dive into the AgentModule interface that critics inspect.
Critics and Stop Criteria
How critics interact with stop criteria and retry budgets.
