Rubric Middleware
Rubric middleware is a self-evaluation system that detects when an agent is making insufficient progress against a goal. Unlike count-based loop detection, it uses a grader sub-agent to evaluate the transcript against criteria — catching futile loops where the agent appears active but isn't converging.
How it works
RubricMiddleware is appended to the middleware stack when rubric is enabled via configuration. It activates only when a rubric is present in the invocation state — without one, both before_agent and after_agent return immediately (zero overhead).
When active:
- After each agent turn, the grader sub-agent evaluates the transcript against the rubric criteria.
- If criteria are satisfied → agent terminates normally.
- If revision needed → a
HumanMessagewith feedback is injected and the agent loops back to model. - If
max_iterationsreached without satisfaction → agent terminates with statusmax_iterations_reached.
Configuration
Project-level (abt.yaml)
config:
middleware:
- rubric:
max_iterations: 5 # default: 3, range [1, 20]
grader_model_id: claude-haiku-4-5 # optional: cheaper model for grading
grader_system_prompt: | # optional: custom grading instructions
You are a strict grader. Only mark criteria as satisfied
when there is clear evidence in the transcript.
max_transcript_messages: 60 # optional: default 30, range [1, 200], or -1 for unlimited
max_transcript_chars_per_message: 12000 # optional: default 4000, range [500, 50000], or -1 for unlimited
Transcript truncation
The grader only sees a bounded slice of the transcript: the last max_transcript_messages
messages (plus the original user prompt), each cut to max_transcript_chars_per_message
characters with a trailing ...(truncated) marker. The defaults (30 messages / 4,000
chars per message) are tuned for short back-and-forth transcripts.
Agents that produce long final answers (e.g. a markdown report with tables) can blow past
the 4,000-char default well before the answer is fully written — the grader then sees a
truncated report and may report it as incomplete even though it is not. If your grader
verdicts mention (truncated) or a report cut off mid-sentence, raise
max_transcript_chars_per_message (and max_transcript_messages if the transcript itself
has many turns) rather than shortening the agent's actual answer.
Set either to -1 to disable truncation on that dimension entirely (the grader then sees
the full transcript / full message text, at the cost of a larger, more expensive grading call).
max_transcript_chars_per_message: -1 is low risk: the grader just receives the same
message content the agent's own context already held, at higher cost per grading call
(repeated up to max_iterations times).
max_transcript_messages: -1 is riskier: it is not bounded by anything the agent's own
context held. If abt's compaction middleware is configured on the agent,
state["messages"] stays bounded upstream and the grader inherits that bound regardless of
this setting. Without compaction (or with a long tool-call history that compaction hasn't
caught up on yet), the grader sees the entire raw message list, which can exceed the
grader's own context window — especially if grader_model_id points to a smaller model
than the executor. If you combine -1 with compaction, list compaction before rubric
in your middleware: config so the grader sees the compacted transcript.
Per-agent frontmatter
An agent middleware: list overrides the project entry by name. In the agent's frontmatter, add:
middleware:
- rubric:
max_iterations: 8
grader_system_prompt: "Grade SQL query quality against the rubric criteria."
Enabling it with defaults does nothing until a rubric is set (see Passing a rubric). In the agent's frontmatter, add:
middleware:
- rubric: {}
Passing a rubric
The middleware reads its criteria from a rubric key on the LangGraph invocation state. Without one, before_agent/after_agent no-op, so enabling the middleware alone (as shown above) has no effect until something sets rubric.
abt run and abt dispatch take no rubric flag, and the SDK takes no rubric argument. Set a static rubric config key instead. abt seeds it into the state automatically, so a config-declared rubric works without any caller changes. In the agent's frontmatter, add:
middleware:
- rubric:
max_iterations: 2
grader_model_id: claude-haiku-4-5
rubric: |
1. Is the answer complete?
2. Does the answer have hard evidence for everything it claims?
The seed middleware only fills rubric when the state doesn't already have one, so a caller/graph that sets rubric explicitly still takes priority over the static config value.
Configuration parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
max_iterations |
int |
3 |
Max grader evaluation cycles before giving up (range [1, 20]) |
grader_model_id |
str |
agent's model | Model for the grader sub-agent (use a cheaper model for cost savings) |
grader_system_prompt |
str |
built-in | Custom grading instructions for domain-specific criteria |
rubric |
str |
none | Static rubric criteria seeded into state before the grader runs (see above) |
max_transcript_messages |
int |
30 |
Max trailing messages (plus original prompt) included in the grader's transcript (range [1, 200], or -1 for unlimited) |
max_transcript_chars_per_message |
int |
4000 |
Max characters kept per message before truncation (range [500, 50000], or -1 for unlimited) — see Transcript truncation |
By default the grader uses the same model as the executor agent. Set grader_model_id to a cheaper/faster model — the grader only evaluates text against criteria, it doesn't need to reason as deeply as the executor.
Relationship to loop detection
| Mechanism | Detects | Method |
|---|---|---|
| Loop detection | Repetitive patterns | Message-scanning heuristics |
| Rubric middleware | Lack of progress toward goal | LLM grader evaluation |
| Token budget | Resource exhaustion | Cumulative token counting |
Rubric middleware is complementary to loop detection — it catches cases where the agent makes varied tool calls (avoiding pattern detection) but still isn't converging on the goal.
Structured output and revisions
An agent with output_schema prints its JSON answer when the run ends, so a draft the grader rejects is not shown. A run stopped early (timeout, recursion limit) prints the latest structured draft, which the grader may not have judged.
Observing termination status
When the rubric grader terminates without satisfied:
_rubric_statuson the returned state contains the reason (failed,max_iterations_reached,grader_error)- A
logger.warningis emitted formax_iterations_reached - Stream events
rubric_evaluation_startandrubric_evaluation_endare emitted for observability
State fields
The middleware tracks its state via internal graph fields (not visible in input/output schema):
| Field | Purpose |
|---|---|
rubric |
Caller-supplied criteria string (public input) |
_rubric_status |
Current status (satisfied, needs_revision, failed, etc.) |
_rubric_iterations |
Number of grader cycles completed |
_rubric_evaluations |
List of all evaluation results |