Skip to content

Add user-configurable agent overrides and graceful maxTurns handling #483

Closed

Description

@admarble

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: /spec Explore agents (context gathering) use maxTurns: 10
  • AC-2: /qa quality check agents (type safety, scope/size, security) use maxTurns: 15
  • AC-3: /qa docs-only fast-path agent uses maxTurns: 12
  • AC-4: /exec implementation agents use maxTurns: 40
  • AC-5: /testgen agents use maxTurns: 10
  • AC-6: All three skill directories are synced: .claude/skills/, templates/skills/, skills/

2. effort tuning on routine agents (P0)

  • AC-7: /qa quality check agents include effort: low (type safety, scope/size, security scan are routine pattern-matching tasks)
  • AC-8: /spec Explore agents keep default effort (context gathering benefits from deeper reasoning)
  • AC-9: /exec implementation agents keep default effort (code generation needs full reasoning)

3. User-configurable overrides (P1)

  • AC-10: .sequant/settings.json schema supports agents.maxTurns (number, overrides skill defaults)
  • AC-11: .sequant/settings.json schema supports agents.effort ("low" | "medium" | "high")
  • AC-12: Settings are documented in src/lib/settings.ts with DEFAULT_AGENT_SETTINGS
  • AC-13: CLI flags --max-turns N and --effort low|medium|high on sequant run pass through to skills

4. Graceful maxTurns handling (P1)

  • AC-14: When ClaudeCodeDriver receives error_max_turns, it logs a warning (not error) and returns partial output
  • AC-15: /qa skill treats a turn-capped check as "inconclusive" — flags it in the QA summary rather than failing the entire QA phase
  • AC-16: /exec skill 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.md documents maxTurns and effort fields 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.maxTurns vs agents.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 sites
  • templates/skills/ — mirror all changes
  • skills/ — mirror all changes
  • .claude/skills/_shared/references/subagent-types.md — document new fields
  • src/lib/settings.ts — extend AgentSettings interface and defaults
  • src/lib/workflow/drivers/claude-code.ts — graceful error_max_turns handling
  • bin/cli.ts — add --max-turns and --effort options

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: maxTurns field 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

  1. added
    enhancement
    planned
    multi-agent
    on Apr 5, 2026
  2. admarble commented on Apr 6, 2026

    @admarble
    ContributorAuthor

    → 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

  3. admarble commented on Apr 12, 2026

    @admarble
    ContributorAuthor

    → 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)

  4. admarble commented on Apr 12, 2026

    @admarble
    ContributorAuthor

    Status Update (2026-04-12)

    P0 (AC-1–9) completed by #484. The declarative agent migration shipped maxTurns and effort in .claude/agents/*.md frontmatter:

    Agent #483 proposed #484 shipped
    sequant-explorer maxTurns: 10 maxTurns: 15
    sequant-qa-checker maxTurns: 15, effort: low maxTurns: 15, effort: low
    sequant-implementer maxTurns: 40 maxTurns: 25
    sequant-testgen maxTurns: 10 maxTurns: 25

    Accepting #484's shipped values as baseline. Value tuning (if needed) is a separate conversation.

    Remaining scope (AC-10–18):

    • AC-10–13: Settings overrides (agents.maxTurns, agents.effort) + CLI flags (--max-turns, --effort)
    • AC-14–16: Graceful error_max_turns handling (partial results instead of hard failure)
    • AC-17–18: subagent-types.md reference docs update
  5. changed the title [-]Add token cost guardrails: maxTurns caps and effort tuning for subagents[/-] [+]Add user-configurable agent overrides and graceful maxTurns handling[/+] on Apr 12, 2026
  6. added
    backlog
    on Apr 15, 2026
  7. admarble commented on May 12, 2026

    @admarble
    ContributorAuthor

    Surfacing data from #632 investigation that affects this issue's scope.

    Community baseline contradicts the maxTurns / effort cost argument

    Sampled 144 agents in VoltAgent/awesome-claude-code-subagents (19.6k stars, the dominant community reference):

    Field Coverage
    name, description, tools, model 144 / 144
    maxTurns 0 / 144
    effort 0 / 144
    permissionMode, isolation 0 / 144

    Zero adoption of either field, including in agent prompt bodies — grep across the full 1.9 MB repo returns no mentions of maxTurns, max_turns, or max-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.model override pointless today

    Per #632 / anthropics/claude-code#43869: every documented mechanism for routing subagents to a specific model is silently ignored — including the CLAUDE_CODE_SUBAGENT_MODEL env 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:

    • AC-14: graceful error_max_turns handling in ClaudeCodeDriver — defensive code, low cost, useful regardless of whether anyone sets maxTurns. Probably still worth landing.
    • AC-15/AC-16: turn-capped agents reported as inconclusive/incomplete rather than failing the phase — same defensive rationale.

    Drop or defer:

    • AC-1 through AC-9: setting maxTurns and effort on every spawn site. No community evidence this matters; would diverge from baseline without payoff data.
    • AC-10 through AC-13: agents.maxTurns / agents.effort / agents.model settings + CLI flags. Routing layer is broken upstream; we'd build a setting that does nothing for model:. For maxTurns/effort, see above.
    • AC-17: subagent reference update — fold into fix(agents): realign declared model tiers; document upstream routing bug #632's docs AC instead.

    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_turns handling.

  8. admarble commented on Jun 20, 2026

    @admarble
    ContributorAuthor

    Closing — scope is fully accounted for across other work:

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    backlogenhancementmulti-agentplanned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions