IsStepCount
Returns a StopCondition that stops the tool-calling loop once exactly n steps have
completed (TS isStepCount). StepCountIs is the deprecated name for the same function
and still works. Pass it to StopWhen in GenerateTextOptions or AgentConfig.
Signature
func IsStepCount(n int) StopCondition
// Deprecated: use IsStepCount.
func StepCountIs(n int) StopCondition
Parameters
| Parameter | Type | Description |
|---|---|---|
| n | int | Stop when len(state.Steps) == n |
Return Value
A StopCondition function. When the condition fires it returns the reason string
"maximum number of steps (n) reached"; otherwise it returns "".
type StopCondition func(state StopConditionState) string
Behavior
IsStepCount(n) checks len(steps) == n, the same exact match as TypeScript's
isStepCount(n) (steps.length === n). Stop conditions are evaluated after every
step and the step count grows by one each time, so the threshold is never skipped.
Examples
Basic ceiling
result, err := ai.GenerateText(ctx, ai.GenerateTextOptions{
Model: model,
Prompt: "Analyze the dataset and create a summary",
Tools: tools,
StopWhen: []ai.StopCondition{
ai.IsStepCount(5),
},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(result.Text)
fmt.Printf("Stopped because: %s\n", result.StopReason)
// → "Stopped because: maximum number of steps (5) reached"
Combined with HasToolCall (safety ceiling pattern)
Place HasToolCall first so the semantic completion signal fires before the hard
ceiling. Because EvaluateStopConditions runs all conditions before returning
the first match, both conditions always execute regardless of order.
StopWhen: []ai.StopCondition{
ai.HasToolCall("finish"), // semantic completion
ai.IsStepCount(10), // hard limit fallback
},
With ToolLoopAgent
myAgent := agent.NewToolLoopAgent(agent.AgentConfig{
Model: model,
Tools: tools,
StopWhen: []ai.StopCondition{
ai.IsStepCount(8),
},
})
result, err := myAgent.Execute(ctx, "Complete the task")
fmt.Printf("Steps: %d, StopReason: %s\n", len(result.Steps), result.StopReason)
Defaults
| Scenario | Behavior |
|---|---|
Neither StopWhen nor MaxSteps set | GenerateText / StreamText default to IsStepCount(1); agent.NewToolLoopAgent defaults to IsStepCount(20) |
MaxSteps: &n set, StopWhen not set | Converts to StopWhen{IsStepCount(n)} |
| Both set | StopWhen takes precedence |
See Also
- HasToolCall — stop when a specific tool is called
- Loop Control — full guide with examples
- GenerateText —
GenerateTextOptions.StopWhenfield - ToolLoopAgent —
AgentConfig.StopWhenfield