Skip to content

fix(session): cap OpenAI Responses tool count to avoid 500 server_error loop - #32998

Closed
sjawhar wants to merge 1 commit into
anomalyco:devfrom
sjawhar:fix/openai-responses-tool-limit
Closed

sjawhar wants to merge 1 commit into
anomalyco:devfrom
sjawhar:fix/openai-responses-tool-limit

Conversation

@sjawhar

@sjawhar sjawhar commented Jun 19, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #33006

Type of change

  • Bug fix

What does this PR do?

When many tools are enabled (lots of MCP servers), opencode sends every tool definition in a single OpenAI Responses request. The ChatGPT/Codex OAuth backend rejects requests carrying too many tools with a generic 500 server_error, and because the retry policy has no attempt cap for 5xx it loops indefinitely — which makes gpt-5.5 effectively unusable.

This caps the tool set at 128 (OpenAI's documented function ceiling) for @ai-sdk/openai models, right after LLMRequestPrep.prepare. Built-in/core tools are registered first, so the slice keeps those and drops the long MCP tail, logging a warning when it trims. 128 is conservative — happy to change the number or make it provider-configurable.

Note: the experimental native runtime (openai-responses.ts) uses input.tools rather than prepared.tools, so it isn't covered here — can extend if wanted. A complementary fix would be a max-attempt cap for 5xx in the retry policy so any future provider 500 can't loop forever.

How did you verify your code works?

Reproduced with gpt-5.5 + 876 tool definitions: 5 server_errors and a stuck loop. The same prompt with --pure (11 tools) works, confirming tool count is the trigger (not schema, size, or reasoning threading). After the cap: bun typecheck clean in packages/opencode, and running from source against the same 876-tool setup logs capping total=876 kept=128, completes a full tool round-trip, and exits with server_errors=0.

Screenshots / recordings

N/A — no UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Jun 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions github-actions Bot removed needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels Jun 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@codeg-dev

Copy link
Copy Markdown

Thanks for isolating this. I hit the same failure mode locally with a very large tool surface while working on a separate stale-continuation recovery patch.

I intentionally did not open a duplicate PR for the OpenAI Responses tool-count cap because this PR already covers that part. In my local validation, capping the submitted tool definitions separately from the stale-continuation/model-recovery fix was the right split: the tool cap prevents the OpenAI 500/server_error loop, while the continuation fix prevents internal retry/fallback rows from becoming the durable session model.

Relevant local checks from the separated branch:

  • app session-model/timeline tests: 10 pass
  • opencode retry tests: 37 pass
  • app/opencode typecheck pass
  • pre-push bun turbo typecheck: 23/23 pass

Prepared with AI assistance.

@trajectory-s-legion-implementer
trajectory-s-legion-implementer Bot force-pushed the fix/openai-responses-tool-limit branch from 69fa246 to b9cc4fb Compare June 21, 2026 23:42
@trajectory-s-legion-implementer
trajectory-s-legion-implementer Bot force-pushed the fix/openai-responses-tool-limit branch from b9cc4fb to 7b6d727 Compare July 3, 2026 17:45
@sjawhar
sjawhar force-pushed the fix/openai-responses-tool-limit branch 2 times, most recently from 7821113 to 2b4c9cb Compare July 13, 2026 17:01
@trajectory-s-legion-implementer
trajectory-s-legion-implementer Bot force-pushed the fix/openai-responses-tool-limit branch from 2b4c9cb to 26933e3 Compare July 17, 2026 17:44
@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAI Responses (gpt-5.5): 500 server_error doom-loop when too many tools are enabled

2 participants