You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Agent loop stops after first LLM call when using @ai-sdk/openai-compatible with LiteLLM (finish_reason: stop instead of tool_calls) #20719
When using the opencode_local adapter with any OpenAI-compatible provider (LiteLLM proxy in front of Ollama), the agent loop exits after exactly one LLM call per run — before any tool is called.
The model receives the prompt correctly and produces output tokens, but OpenCode terminates the session with finish_reason: stop instead of continuing the agentic loop.
Root cause
OpenCode's loop exit condition in session/prompt.ts checks finish_reason. The OpenAI spec returns "tool_calls" when a tool was invoked, but @ai-sdk/openai-compatible providers (including LiteLLM) return "stop" even when the response logically should continue. This causes the loop to break after every single LLM call — before any tool is ever executed.
This was previously reported in #14972 but appears to still affect OpenCode 1.3.13 with @ai-sdk/openai-compatible.
Steps to reproduce
Set up OpenCode with @ai-sdk/openai-compatible pointing to LiteLLM (https://your-litellm-host/v1)
LiteLLM routes to Ollama with custom_llm_provider: ollama_chat
Run any prompt that should trigger tool use
Observe: OpenCode exits after the first LLM response, no tool is called
Observed behavior
Each run produces output tokens but zero tool calls. LiteLLM logs confirm:
Tools: 10 provided, 0 called
finish_reason: "stop"
The agent requires 10–20+ separate run invocations to complete what should be a 3–5 step task.
Expected behavior
The agent loop should continue after each LLM response until a tool is called, the model signals completion, or max_steps is reached.
Additional context
Direct curl test to Ollama confirms tool calling works correctly: finish_reason: "tool_calls" is returned when tools are provided
The bug only manifests through the @ai-sdk/openai-compatible → LiteLLM → Ollama chain
OpenCode 1.3.13 also sends parallel_tool_calls which ollama_chat does not support — requires drop_params: true in LiteLLM as a workaround
Tested with multiple models (qwen3:32b, qwen3.5:122b) — all exhibit the same behavior
Suggested fix
In session/prompt.ts, treat finish_reason: "stop" as non-terminal when the response also contains tool calls. The presence of tool calls in the response should take precedence over finish_reason when deciding whether to continue the loop.
Alternatively, expose a config option to treat "stop" as "tool_calls" for providers known to return incorrect finish_reason values.
Activity
github-actions commentedon Apr 2, 2026
This issue might be a duplicate of existing issues. Please check:
finish_reason: "stop"returned by OpenAI-compatible providers (LiteLLM, Gemini) causing premature agent loop exit insession/prompt.ts. Closed but apparently still affects v1.3.13.finish_reasonandtool_callshandling.github-actions commentedon Jul 4, 2026
To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.