Skip to content

Conversation

@nwthomas
Copy link
Contributor

@nwthomas nwthomas commented Nov 8, 2025

Problem

See #3631 for more details about this feature. From the issue:

"We want to add a maxSteps constraint to prevent costly large iteration loops for the supervisor and sub-agents.

On the maxSteps - 1 loop iteration, it should kick out of any sub-agent tool calls and return a text response."

Solution

I went nine rounds in the ring with the AI harness for OpenCode and figuring out the most consistent way to enforce this behavior across many models (tried mainly Grok code Fast, Big Pickle, and GPT OSS 20B and Qwen3 30B which I am locally hosting).

I tried removing tools from the calls on the final step(s) and paired it with a system prompt, but I consistently had models ignore the system prompt and even lack of activeTools and tools values, resulting in breaking attempts to call tools that were not defined.

I also tried the Vercel AI SDK toolChoice param (since we're using that package), but I also found that models universally ignored that request.

What did work every time was conditionally placing both a system prompt and an assistant prompt on the last step. With both of these combined, all models I've tested respect the maxSteps limit and instantly return a text-based summary of actions taken and recommended next steps.

I've also taken the liberty of updating the website documentation for this feature.

Testing

Windows MacOS Linux
With maxSteps = 4
Screen.Recording.2025-11-17.at.6.39.07.PM.mov
Screen.Recording.2025-11-17.at.6.49.45.PM.mov
Screencast.from.2025-11-17.19-08-05.webm
Without maxSteps in config
Screen.Recording.2025-11-17.at.6.40.15.PM.mov
Screen.Recording.2025-11-17.at.6.51.02.PM.mov
Screencast.from.2025-11-17.19-11-20.webm

@nwthomas nwthomas force-pushed the feat/allow-max-steps-for-agents branch 2 times, most recently from dfdd6df to 25a17a2 Compare November 8, 2025 06:42
@nwthomas nwthomas force-pushed the feat/allow-max-steps-for-agents branch 26 times, most recently from 0e1166f to eb6ad55 Compare November 18, 2025 01:19
@@ -0,0 +1,16 @@
CRITICAL - MAXIMUM STEPS REACHED
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used for both the system prompt concat and also the new conditional assistant message

system.push(...(await SystemPrompt.environment()))
system.push(...(await SystemPrompt.custom()))

if (input.isLastStep) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conditional addition of the system prompt

return false
}),
),
...(isLastStep
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conditional addition of a new `assistant message. See PR description for more logic about why I went this route.


// normal processing
const agent = await Agent.get(lastUser.agent)
const maxSteps = agent.maxSteps ?? Infinity
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If maxSteps is undefined, this value will not cap agentic actions

@nwthomas nwthomas force-pushed the feat/allow-max-steps-for-agents branch 2 times, most recently from e8dbab4 to c60a030 Compare November 20, 2025 20:08
@github-actions github-actions bot force-pushed the dev branch 2 times, most recently from 917250b to f4593c6 Compare November 22, 2025 05:02
@nwthomas nwthomas force-pushed the feat/allow-max-steps-for-agents branch from c30ae17 to c60a030 Compare November 22, 2025 09:17
@github-actions github-actions bot force-pushed the dev branch 2 times, most recently from df8bdf9 to 0dd5039 Compare November 22, 2025 18:21
@nwthomas nwthomas force-pushed the feat/allow-max-steps-for-agents branch 2 times, most recently from 649991d to ae2c890 Compare November 25, 2025 06:33
@nwthomas nwthomas force-pushed the feat/allow-max-steps-for-agents branch 3 times, most recently from e0eb99f to 7731405 Compare December 3, 2025 02:11
@nwthomas nwthomas force-pushed the feat/allow-max-steps-for-agents branch from 7731405 to d19e28f Compare December 5, 2025 17:56
@rekram1-node
Copy link
Collaborator

/review

We want a way to limit the number of steps allow
agents in the app can take. This is primarily useful
for sub-agents, but it applies to the supervisor as
well.

The user can define a maxSteps value for each agent.

Documentation has also been updated for this new feature.
@nwthomas nwthomas force-pushed the feat/allow-max-steps-for-agents branch from d19e28f to 76fcbf0 Compare December 5, 2025 18:01
@rekram1-node
Copy link
Collaborator

/oc use docs agent to update the example for max_steps it should only show an example for a subagent, dont show the setting being applied to general, plan or build

@opencode-agent
Copy link
Contributor

opencode-agent bot commented Dec 5, 2025

Done! The docs agent has updated the max_steps example in packages/web/src/content/docs/agents.mdx to only show it being applied to a subagent (the "general" subagent). Examples showing max_steps applied to general, plan, or build primary agents have been removed.

New%20session%20-%202025-12-05T18%3A10%3A44.000Z
opencode session  |  github run

@rekram1-node
Copy link
Collaborator

/oc use docs agent to update the max steps example to show a custom agent w/ max steps set, does have the example use any default agent we provide, this custom agent can be completely stupid it doesnt have to be real and the prompt for it can be super super simple too

@opencode-agent
Copy link
Contributor

opencode-agent bot commented Dec 5, 2025

Perfect! The example has been successfully updated. The max steps section now shows:

A custom agent named quick-thinker (not using any default agents like "general", "build", or "plan")
A simple prompt - "You are a quick thinker. Solve problems with minimal steps."
maxSteps set to 5 - demonstrating the configuration
A description - "Fast reasoning with limited iterations"

The explanation text about what max steps does remains unchanged, and this is now the only example shown for this feature.

New%20session%20-%202025-12-05T18%3A15%3A58.135Z
opencode session  |  github run

@rekram1-node rekram1-node merged commit 40eb8b9 into anomalyco:dev Dec 5, 2025
3 checks passed
shuv1337 added a commit to Latitudes-Dev/shuvcode that referenced this pull request Dec 5, 2025
* fix: ensure checkUpgrade sets `init:` (anomalyco#5040)

* chore: format code

* ci: use blacksmith runners in review workflow too (anomalyco#5042)

* zen: make session provider sticky

* ci: only maintainer can trigger

* ci: cleaner

* ignore: update download stats 2025-12-04

* fix(tui): cursor color

* Preserve prompt input when creating new session (anomalyco#4993)

* fix: model not being passed correctly to tool

* tweak: bash tool messages regarding timeouts and truncation more clear for agent (anomalyco#5066)

* tui: fix /new slash command being persisted in prompt input

* add optional prompt Input to Github Action (anomalyco#4828)

Co-authored-by: Github Action <[email protected]>
Co-authored-by: Aiden Cline <[email protected]>
Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>

* bump: builtin plugin versions

* feat: Add SAP AI Core provider support (anomalyco#5023)

Signed-off-by: Jérôme Benoit <[email protected]>

* core: ensure model npm package falls back to dev models config when not explicitly defined

* docs: add CodeCompanion.nvim integration instructions (anomalyco#5079)

* tweak: bash tool description re commit stuff

* core: add test for custom model npm package inheritance

* feat(desktop): terminal pane (anomalyco#5081)

Co-authored-by: Github Action <[email protected]>
Co-authored-by: Dax Raad <[email protected]>

* Revert "feat(desktop): terminal pane (anomalyco#5081)"

This reverts commit d763c11.

* release: v1.0.133

* Update Nix flake.lock and hashes

* Reapply "feat(desktop): terminal pane (anomalyco#5081)"

This reverts commit f9dcd97.

* chore: format code

* Update Nix flake.lock and hashes

* zen: fix byok

* ignore: fix provider credentials query for BYOK

Provider credentials field was being selected from ProviderTable even when the table wasn't joined (when byokProvider was undefined). Now the join is conditional - when byokProvider exists, we join and get the credentials; when it doesn't, the join condition is always false so provider remains null.

* docs: llama.cpp docs: `limit` moved under model (anomalyco#5089)

Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Co-authored-by: GitHub Action <[email protected]>

* fix: add getModel to SAP AI Core provider for correct SDK initialization (anomalyco#5086)

Signed-off-by: Jérôme Benoit <[email protected]>

* fix pty builds

* fix

* increase default scroll speed

* ci: review

* fix: ensure that vcs is still set to git even if no commits in repo

* ignore: rm slop commnand (only for opencode repo this isnt shipping)

* chore: format code

* ignore: cmd tweak

* tui: wrap dialog option descriptions (anomalyco#5083)

* ci: keybinds

* fix: ensure projects that go from having no commits to having commits have sessions migrated (anomalyco#5105)

Co-authored-by: GitHub Action <[email protected]>

* ignore: tweak

* ignore: update download stats 2025-12-05

* Zen: add codex max

* do not use required pty for local dev

* fix: anomalyco#5064 ProviderInitError github-copilot-enterprise (anomalyco#5123)

* feat(theme): Vercel (anomalyco#5119)

Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Co-authored-by: rekram1-node <[email protected]>

* fix(desktop): new session not selecting tab

* fix(desktop): clone pty session on reconnect

* add experimental.open_telemetry config option to enable OTEL spans (anomalyco#4978)

Co-authored-by: noamzbr <[email protected]>
Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Co-authored-by: rekram1-node <[email protected]>

* ignore: regen sdk

* fix(desktop): prompting

* fix(desktop): terminal cursor position

* feat: add max steps for supervisor and sub-agents (anomalyco#4062)

Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Co-authored-by: rekram1-node <[email protected]>

* tweak: bash tool improve output metadata for agent consumption, fix small timeout issue (anomalyco#5131)

* OpenCode Desktop app (anomalyco#5044)

Co-authored-by: Adam <[email protected]>

* Update Nix flake.lock and hashes

* release: v1.0.134

* sync: record last synced tag v1.0.134

* chore: format code

---------

Signed-off-by: Jérôme Benoit <[email protected]>
Co-authored-by: YeonGyu-Kim <[email protected]>
Co-authored-by: GitHub Action <[email protected]>
Co-authored-by: wsx99outlook <[email protected]>
Co-authored-by: Frank <[email protected]>
Co-authored-by: Aiden Cline <[email protected]>
Co-authored-by: Adam <[email protected]>
Co-authored-by: Daniel Gray <[email protected]>
Co-authored-by: Shantur Rathore <[email protected]>
Co-authored-by: Daniel Polito <[email protected]>
Co-authored-by: Aiden Cline <[email protected]>
Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Co-authored-by: Jérôme Benoit <[email protected]>
Co-authored-by: Dax Raad <[email protected]>
Co-authored-by: Cason Adams <[email protected]>
Co-authored-by: opencode <[email protected]>
Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>
Co-authored-by: ry2009 <[email protected]>
Co-authored-by: Julian Visser <[email protected]>
Co-authored-by: Anthony Shew <[email protected]>
Co-authored-by: rekram1-node <[email protected]>
Co-authored-by: Noam Bressler <[email protected]>
Co-authored-by: noamzbr <[email protected]>
Co-authored-by: Nathan Thomas <[email protected]>
Co-authored-by: Brendan Allan <[email protected]>
remorses pushed a commit to remorses/opencode-fork that referenced this pull request Dec 6, 2025
Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Co-authored-by: rekram1-node <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants