Skip to main content

Convergence

Convergence is the process by which AgentGate determines when an agent has successfully completed a task. Instead of simply running a fixed number of iterations, convergence strategies intelligently decide when to continue iterating, when to stop, and how to interpret progress.

What Is Convergence

Traditional automation runs for a predetermined number of attempts. Convergence goes further by:
  • Detecting completion: Recognizing when the agent has achieved the desired state
  • Measuring progress: Tracking improvements across iterations
  • Preventing loops: Identifying when the agent is stuck in repetitive behavior
  • Optimizing cost: Stopping when further iterations are unlikely to help

Convergence Strategies

AgentGate supports five convergence strategies:
Run exactly N iterationsThe simplest strategy. Runs a predetermined number of iterations regardless of outcome.
convergence:
  strategy: fixed
  config:
    iterations: 5
Best for:
  • Simple tasks with predictable completion
  • Cost-controlled environments
  • Testing and debugging
Limitations:
  • May waste iterations on already-completed tasks
  • May stop before completion on complex tasks

Configuration Options

Strategy-Specific Options

StrategyOptionTypeDefaultDescription
fixediterationsnumber3Exact iteration count
hybridbaseIterationsnumber3Guaranteed iterations
hybridbonusIterationsnumber2Extra iterations if progressing
hybridprogressThresholdnumber0.7Progress score needed (0-1)
ralphconvergenceThresholdnumber0.05Similarity threshold
ralphwindowSizenumber3Outputs to compare
ralphminIterationsnumber1Minimum before termination
ralphpromptHotReloadbooleanfalseAllow prompt updates

Resource Limits

Every convergence configuration should include limits:
convergence:
  strategy: hybrid
  limits:
    maxIterations: 10        # Hard cap on iterations
    maxWallClock: "2h"       # Maximum elapsed time
    maxCost: "$50"           # Maximum cost budget
    maxTokens: 1000000       # Total token budget
Always set maxIterations to prevent runaway costs. The default of 100 may be too high for your use case.

Convergence State

During execution, AgentGate tracks convergence state:
interface ConvergenceState {
  iteration: number;           // Current iteration (1-based)
  elapsed: number;             // Elapsed time in milliseconds
  gateResults: GateResult[];   // Results from current iteration
  history: IterationHistory[]; // Previous iteration data
  trend: 'improving' | 'stagnant' | 'regressing';
}
You can query this state via the API:
curl https://agentgate.mynewapi.com/v1/runs/run_123/strategy-state \
  -H "Authorization: Bearer YOUR_API_KEY"

Convergence Decisions

Each iteration ends with a convergence decision:
{
  "continue": true,
  "reason": "Progress detected: 3/5 gates now passing (was 1/5)",
  "confidence": 0.85,
  "metadata": {
    "gatesPassing": 3,
    "gatesTotal": 5,
    "progressScore": 0.6
  }
}
FieldDescription
continueWhether to run another iteration
reasonHuman-readable explanation
confidenceHow confident the strategy is (0-1)
metadataStrategy-specific details

Progress Metrics

AgentGate calculates progress metrics across iterations:
{
  "overall": 0.75,
  "byGate": {
    "lint": { "currentLevel": 1.0, "previousLevel": 0.8, "trend": "improving" },
    "tests": { "currentLevel": 0.5, "previousLevel": 0.3, "trend": "improving" },
    "ci": { "currentLevel": 0.0, "previousLevel": 0.0, "trend": "stagnant" }
  },
  "trend": "improving",
  "velocity": 0.15
}
TrendMeaningAction
improvingGates passing at increasing rateContinue iterations
stagnantNo change in gate passageConsider stopping
regressingGates that passed are now failingInvestigate agent behavior

Choosing a Strategy

Use fixed when:
  • You have a well-understood task
  • Cost predictability is important
  • You’re testing or debugging
  • Tasks typically complete in 1-3 iterations
convergence:
  strategy: fixed
  config:
    iterations: 3
  limits:
    maxIterations: 5
Use hybrid when:
  • Tasks have variable complexity
  • You want to balance cost and completion
  • Progress is measurable through gates
  • Most common choice for production
convergence:
  strategy: hybrid
  config:
    baseIterations: 3
    bonusIterations: 5
    progressThreshold: 0.6
  limits:
    maxIterations: 15
    maxWallClock: "1h"
Use ralph when:
  • Tasks are complex or open-ended
  • Agent needs flexibility to explore
  • Loop detection is important
  • You trust the agent’s judgment
convergence:
  strategy: ralph
  config:
    convergenceThreshold: 0.05
    windowSize: 4
    minIterations: 2
  limits:
    maxIterations: 50
    maxWallClock: "4h"
    maxCost: "$100"
Use manual when:
  • Changes require human approval
  • Learning how the agent behaves
  • Compliance requires oversight
  • High-risk or sensitive code
convergence:
  strategy: manual
  limits:
    maxIterations: 20
    maxWallClock: "24h"

Convergence with Gates

Convergence strategies work with gates to determine completion:
convergence:
  strategy: hybrid
  config:
    baseIterations: 3
    progressThreshold: 0.7

  gates:
    - name: lint
      check:
        type: verification-levels
        levels: [L0, L1]
      onFailure:
        action: iterate

    - name: tests
      check:
        type: verification-levels
        levels: [L2]
      onFailure:
        action: iterate

    - name: ci
      check:
        type: github-actions
      onFailure:
        action: stop  # Don't retry CI failures

Gate Interaction

  1. All gates pass → Task complete, stop iterating
  2. Some gates fail with iterate → Generate feedback, continue
  3. Any gate fails with stop → Task failed, stop immediately
  4. Limits reached → Task failed, stop with partial results

Best Practices

1

Start with Hybrid

For most tasks, hybrid provides the best balance. Start with conservative settings and adjust based on results.
strategy: hybrid
config:
  baseIterations: 3
  bonusIterations: 3
  progressThreshold: 0.7
2

Set Reasonable Limits

Always set limits to control costs. Consider your task complexity:
Task TypeRecommended Max
Simple fix5 iterations
Feature10-15 iterations
Complex feature20-30 iterations
Major refactor50+ iterations
3

Monitor Progress

Use the strategy state API to monitor convergence:
watch -n 10 'curl -s .../runs/run_123/strategy-state | jq .trend'
4

Adjust Based on Results

After completing several tasks, review convergence patterns:
  • Tasks completing too early? Lower progressThreshold
  • Wasting iterations? Raise progressThreshold
  • Loop detection triggering? Adjust convergenceThreshold