The max-steps guard

The simplest defense against a stuck agent is a counter. Count the iterations, fail when the counter runs out, return a helpful error. The whole defense is just a counter. That is the point.

agent/agent.go
go
func New(llm LLMClient, maxIters int) *Agent {
	if maxIters <= 0 {
		maxIters = 10
	}
	return &Agent{
		LLM:      llm,
		Tools:    BuiltinTools(),
		MaxIters: maxIters,
		System:   defaultSystem,
	}
}

// inside Run()
for iter := 0; iter < a.MaxIters; iter++ {
	// ... loop body
}
return "", fmt.Errorf("agent: exceeded max iterations (%d)", a.MaxIters)

The default of ten is a pragmatic number, small enough to surface broken prompts, large enough for multi-file tasks. Callers that know their workload can set something different.

Quiz: Quiz

Loading practice…