Repository navigation
Description
Problem
Subagents spawned by sequant skills (/spec, /qa, /exec, /testgen) have no turn limits or effort constraints. A subagent that goes on a tangent — exploring irrelevant files, retrying failed approaches, or expanding scope beyond its prompt — burns tokens indefinitely until it either completes or hits the session budget. This is the top cost concern for users who aren't on unlimited plans.
From conversation history:
"the trade off is token usage. How should we handle it for our end users? Everyone isn't on an expensive plan"
The ClaudeCodeDriver already recognizes error_max_turns as a failure subtype (in src/lib/workflow/drivers/claude-code.ts), but no skill currently sets maxTurns. Similarly, Claude Code supports an effort field (low/medium/high/max) that reduces thinking tokens on routine tasks, but sequant doesn't use it.
Current Behavior
All 12 subagent spawn sites across /spec, /qa, /exec, and /testgen skills use:
- No
maxTurns— agents run until natural completion or session budget - Default effort — inherited from parent session (typically
medium) model: "haiku"— already cost-optimized, but haiku with no turn cap on a wrong track still wastes tokens
Desired Behavior
Every subagent spawn site has an appropriate maxTurns cap sized to its task complexity. Routine check agents use effort: low to reduce thinking overhead. Users can override defaults via .sequant/settings.json. When a turn cap is hit, the result is treated as a partial success (not a hard failure) and the parent skill gracefully incorporates whatever the agent produced.
Acceptance Criteria
1. maxTurns on all spawn sites (P0)
- AC-1:
/specExplore agents (context gathering) usemaxTurns: 10 - AC-2:
/qaquality check agents (type safety, scope/size, security) usemaxTurns: 15 - AC-3:
/qadocs-only fast-path agent usesmaxTurns: 12 - AC-4:
/execimplementation agents usemaxTurns: 40 - AC-5:
/testgenagents usemaxTurns: 10 - AC-6: All three skill directories are synced:
.claude/skills/,templates/skills/,skills/
2. effort tuning on routine agents (P0)
- AC-7:
/qaquality check agents includeeffort: low(type safety, scope/size, security scan are routine pattern-matching tasks) - AC-8:
/specExplore agents keep default effort (context gathering benefits from deeper reasoning) - AC-9:
/execimplementation agents keep default effort (code generation needs full reasoning)
3. User-configurable overrides (P1)
- AC-10:
.sequant/settings.jsonschema supportsagents.maxTurns(number, overrides skill defaults) - AC-11:
.sequant/settings.jsonschema supportsagents.effort("low"|"medium"|"high") - AC-12: Settings are documented in
src/lib/settings.tswithDEFAULT_AGENT_SETTINGS - AC-13: CLI flags
--max-turns Nand--effort low|medium|highonsequant runpass through to skills
4. Graceful maxTurns handling (P1)
- AC-14: When
ClaudeCodeDriverreceiveserror_max_turns, it logs a warning (not error) and returns partial output - AC-15:
/qaskill treats a turn-capped check as "inconclusive" — flags it in the QA summary rather than failing the entire QA phase - AC-16:
/execskill treats a turn-capped implementation as "incomplete" — reports which tasks finished vs. which were capped
5. Subagent reference update (P2)
- AC-17:
.claude/skills/_shared/references/subagent-types.mddocumentsmaxTurnsandeffortfields with recommended values per spawn type - AC-18: Reference table includes a "why this value" column explaining the sizing rationale
Design Notes
maxTurns Sizing Rationale
| Spawn Site | Type | Bash? | maxTurns | Reasoning |
|---|---|---|---|---|
| Spec context (Explore) | Read-only | No | 10 | Pattern search + file reads; 5–8 turns typical |
| QA type safety | general-purpose | Yes | 15 | tsc + analysis; needs headroom for error investigation |
| QA scope/size | general-purpose | Yes | 15 | git diff + counting; simple but may chase renamed files |
| QA security | general-purpose | Yes | 15 | npm audit + grep patterns; variable depth |
| QA docs-only | general-purpose | Yes | 12 | Link validation + diff; bounded scope |
| Testgen | general-purpose | No | 10 | Template generation from spec output; predictable |
| Exec implement | general-purpose | Yes | 40 | Multi-file edits + test runs; most complex task |
effort Field Integration
Skill SKILL.md files currently use inline Agent spawning. The effort parameter is passed alongside existing parameters:
Agent(subagent_type="general-purpose", model="haiku", mode="bypassPermissions",
maxTurns=15, effort="low",
prompt="Run type safety checks...")
Settings Schema Extension
export interface AgentSettings {
parallel: boolean;
model: "haiku" | "sonnet" | "opus";
maxTurns?: number; // NEW: override per-skill defaults
effort?: "low" | "medium" | "high"; // NEW: override per-skill defaults
}Graceful Degradation Pattern
When maxTurns is reached, the ClaudeCodeDriver receives error_max_turns. Current behavior: treated as failure. New behavior:
if (result.subtype === 'error_max_turns') {
logger.warn(`Agent hit turn cap (${maxTurns}). Returning partial results.`);
return { status: 'partial', output: result.output, capped: true };
}Skills check result.capped and adjust their summary accordingly.
Out of Scope
- Per-agent-type maxTurns in settings (e.g.,
agents.qa.maxTurnsvsagents.exec.maxTurns) — too granular for v1; single override is sufficient - Budget caps (
maxBudgetUsd) — separate concern, tracked by Claude Code natively - Changing default model from haiku — already optimized
Files Affected
.claude/skills/spec/SKILL.md— add maxTurns to Explore spawn sites.claude/skills/qa/SKILL.md— add maxTurns + effort to quality check spawn sites.claude/skills/exec/SKILL.md— add maxTurns to implementation spawn sites.claude/skills/testgen/SKILL.md— add maxTurns to test generation spawn sitestemplates/skills/— mirror all changesskills/— mirror all changes.claude/skills/_shared/references/subagent-types.md— document new fieldssrc/lib/settings.ts— extendAgentSettingsinterface and defaultssrc/lib/workflow/drivers/claude-code.ts— gracefulerror_max_turnshandlingbin/cli.ts— add--max-turnsand--effortoptions
References
- ClaudeCodeDriver error handling:
src/lib/workflow/drivers/claude-code.ts - Current AgentSettings:
src/lib/settings.ts(line 235) - Subagent types reference:
.claude/skills/_shared/references/subagent-types.md - Claude Code docs:
maxTurnsfield in subagent frontmatter - Related: QA skill: add small-diff fast path to skip sub-agents for trivial changes #465 (small-diff fast path — complementary cost optimization)
Activity
admarble commentedon Apr 6, 2026
→ PROCEED — maxTurns + effort caps for all 12 spawn sites, clear ACs
spec → exec → qa · 18 ACs · -q (multi-file, settings + CLI + skills)
⚠ Heavy file overlap with #484 (SKILL.md × 4, subagent-types.md) — chain after #484
admarble commentedon Apr 12, 2026
→ PROCEED — 18 ACs, maxTurns + effort caps for all spawn sites, #484 dependency resolved (closed)
spec → exec → qa · 18 ACs · -q (multi-file: settings + CLI + 4 skill SKILL.md × 3 dirs)
⚠ Sequence after #502 and #515 (both modify SKILL.md files in shared directories)
admarble commentedon Apr 12, 2026
Status Update (2026-04-12)
P0 (AC-1–9) completed by #484. The declarative agent migration shipped
maxTurnsandeffortin.claude/agents/*.mdfrontmatter:Accepting #484's shipped values as baseline. Value tuning (if needed) is a separate conversation.
Remaining scope (AC-10–18):
agents.maxTurns,agents.effort) + CLI flags (--max-turns,--effort)error_max_turnshandling (partial results instead of hard failure)subagent-types.mdreference docs update[-]Add token cost guardrails: maxTurns caps and effort tuning for subagents[/-][+]Add user-configurable agent overrides and graceful maxTurns handling[/+]admarble commentedon May 12, 2026
Surfacing data from #632 investigation that affects this issue's scope.
Community baseline contradicts the
maxTurns/effortcost argumentSampled 144 agents in VoltAgent/awesome-claude-code-subagents (19.6k stars, the dominant community reference):
name,description,tools,modelmaxTurnseffortpermissionMode,isolationZero adoption of either field, including in agent prompt bodies — grep across the full 1.9 MB repo returns no mentions of
maxTurns,max_turns, ormax-turns. The community's discipline pattern for constraining agents is detailed structured prompts (typical agent file: 56–286 lines, median ~280) — not turn caps.Upstream model-routing bug makes the
agents.modeloverride pointless todayPer #632 /
anthropics/claude-code#43869: every documented mechanism for routing subagents to a specific model is silently ignored — including theCLAUDE_CODE_SUBAGENT_MODELenv var this issue's AC-10/AC-11 would wire up. ACs that depend on model routing should wait for upstream resolution.Suggested rescope
Keep:
error_max_turnshandling inClaudeCodeDriver— defensive code, low cost, useful regardless of whether anyone setsmaxTurns. Probably still worth landing.Drop or defer:
maxTurnsandefforton every spawn site. No community evidence this matters; would diverge from baseline without payoff data.agents.maxTurns/agents.effort/agents.modelsettings + CLI flags. Routing layer is broken upstream; we'd build a setting that does nothing formodel:. FormaxTurns/effort, see above.If we want to preserve the cost-conscious framing, the realistic lever isn't
maxTurns— it's the model tier itself, which #632 addresses (realigning declared tiers so when #43869 lands, the cost win comes automatically). The other practical cost lever already exists: the small-diff fast path (#465) that skips subagents entirely for trivial changes.Happy to redraft this issue's body if you agree with the rescope, or close + open a narrower successor focused only on graceful
error_max_turnshandling.admarble commentedon Jun 20, 2026
Closing — scope is fully accounted for across other work:
maxTurns+effortship in.claude/agents/*.mdfrontmatter (shipped values: explorer 15, qa-checker 15/low, implementer 25, testgen 25).agents.modelrouting is a no-op untilanthropics/claude-code#43869lands; tracked under fix(agents): realign declared model tiers; document upstream routing bug #632. No payoff to building the override layer now.error_max_turnshandling): carved into Graceful error_max_turns handling for subagents #733 — the only live, unblocked slice.Per the 2026-05-12 rescope analysis above, closing in favor of the narrow successor rather than carrying an 18-AC issue that's mostly done or blocked.