Skip to content

Agent loop stops after first LLM call when using @ai-sdk/openai-compatible with LiteLLM (finish_reason: stop instead of tool_calls) #20719

Closed as not planned

Description

@ByteBand1t

Description

Environment

  • OpenCode: 1.3.13
  • Adapter: opencode_local via Paperclip
  • Provider: @ai-sdk/openai-compatible → LiteLLM v1.82.6 → Ollama (ollama_chat)
  • Models tested: qwen3:32b, qwen3.5:122b, glm-4.7-flash, gpt-oss:120b
  • Deployment: Docker/Portainer, self-hosted

Describe the bug

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

  1. Set up OpenCode with @ai-sdk/openai-compatible pointing to LiteLLM (https://your-litellm-host/v1)
  2. LiteLLM routes to Ollama with custom_llm_provider: ollama_chat
  3. Run any prompt that should trigger tool use
  4. 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.

Plugins

No response

OpenCode version

1.3.13

Steps to reproduce

No response

Screenshot and/or share link

No response

Operating System

No response

Terminal

No response

Activity

  1. added
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on Apr 2, 2026
  2. github-actions commented on Apr 2, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

  3. removed
    bugSomething isn't working
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on May 3, 2026
  4. github-actions commented on Jul 4, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions