Skip to content

fix(core): prevent agent loop from stopping after tool calls with OpenAI-compatible providers#14973

Open
valenvivaldi wants to merge 9 commits intoanomalyco:devfrom
valenvivaldi:fix/agent-loop-openai-compatible-tool-calls
Open

fix(core): prevent agent loop from stopping after tool calls with OpenAI-compatible providers#14973
valenvivaldi wants to merge 9 commits intoanomalyco:devfrom
valenvivaldi:fix/agent-loop-openai-compatible-tool-calls

Conversation

@valenvivaldi
Copy link
Copy Markdown

@valenvivaldi valenvivaldi commented Feb 25, 2026

Issue for this PR

Closes #14972
Related: #14063

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Some OpenAI-compatible providers (Gemini, LiteLLM) return finish_reason: "stop" instead of "tool_calls" when their response contains tool calls. This differs from the OpenAI standard.

The agent loop exit condition in prompt.ts (line ~318) only checks the finish reason to decide whether to continue. When it sees "stop", it breaks the loop — even though tools were just executed and need their results processed by the model.

The fix adds one extra check: before exiting the loop, look at the last assistant message parts for any type === "tool" entries. If tool parts exist, the model did call tools, so the loop must continue regardless of what finish_reason the provider reported.

How did you verify your code works?

  • Typecheck passes (16/16 packages)
  • Tested manually running bun run dev with Gemini 3 Flash configured as an OpenAI-compatible provider. Before the fix, the agent stopped after every tool call. After the fix, it continues processing tool results normally.

Screenshots / recordings

N/A — not a UI change.

Checklist

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

…nAI-compatible providers

Some OpenAI-compatible providers (Gemini, LiteLLM) return finish_reason
"stop" instead of "tool_calls" when the response contains tool calls.
This caused the agent loop to exit prematurely after executing a tool,
instead of continuing to process the tool results.

The fix adds a check for tool parts in the last assistant message. If
tool calls were made, the loop continues regardless of the provider's
reported finish_reason.

Fixes anomalyco#14972
Related: anomalyco#14063

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions github-actions bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Feb 25, 2026
@github-actions
Copy link
Copy Markdown
Contributor

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

@valenvivaldi
Copy link
Copy Markdown
Author

Hey everyone, thanks for all the thumbs up! 👍

I've been keeping this branch updated with the latest dev — the bug is still present upstream as of today.

On our end, we need this fix since our team uses a LiteLLM proxy to route requests to Gemini, OpenAI, and other providers. Without it, the agent stops after every tool call, which makes it essentially unusable for agentic workflows.

Great to see this is also helping folks running local LLMs — glad it's not just us!

Hoping a maintainer gets a chance to review this at some point. The fix is minimal (7 lines) and all CI checks pass.

@martin-liu
Copy link
Copy Markdown

@Hona Would appreciate a look when you have a chance.

This seems to affect a broad set of users of OpenAI-compatible providers, especially many companies using proxy layers. Thanks!

@martin-liu
Copy link
Copy Markdown

@rekram1-node The tests failed on your comment-only commit, looks like unrelated flaky tests.

@valenvivaldi
Copy link
Copy Markdown
Author

Hi everyone! Just updated the branch with the latest from dev — all CI checks are passing. 🟢

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.

Agent stops after tool execution with OpenAI-compatible providers (Gemini, LiteLLM)

3 participants