opencode icon indicating copy to clipboard operation
opencode copied to clipboard

How to control Timeouts

Open mrbm opened this issue 1 year ago • 35 comments

I am running ollama on my local network on a dedicaled m4 24gig with Qwen3:14b Quant with 65k context window.

I had it create a slightly more than basic react app and it created things, wrote some of the thngs in the dialog and wrote 50% of the things to disk, the other 50 it didn't I gave it a command to write the rest of the items...and then it did some, but not all of it, i did another command to finish...and now it just timesout.

I am reading the ollama serve logs and noticed that open code is sending a timeout singnal around 4 min ond each time.

So...the issue is opencode.

I didn't set a timeout in the config. Then i tried setting a timeout in the opencode config to 600 seconds, same results.

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "ollama-local-remote": {
      "npm": "@ai-sdk/openai-compatible",
      "options": {
        "baseURL": "http://192.168.XX.XXX:11434/v1",
        "timeout": 600000
      },
      "models": {
        "llama3:8b": {"name": "Ollama Llama3"},
        "qwen3:14b-65k": {
          "name": "qwen3:14b 65k (remote local)",
          "tools": true,
          "reasoning": true,
          "options": { "num_ctx": 65536 }
        },
        "qwen3:14b-q4_K_M": {
          "name": "qwen3:14b-q4_K_M (remote local)",
          "tools": true,
          "reasoning": true
        }

Is this the right way to do it? Or is this something that can't be changed. Thanks! OpenCode.ai special sauce is going to be for those homebuilds which are going to have less resources and as a result opencode needs to be able to wait longer. Love the community thoughts or is this coming soon. I think there was 3 releases yesterday so i know things are happening rapidly.

mrbm avatar Jul 16 '25 18:07 mrbm

hmm im not sure where this timeout is being set - i don't think that timeout option exists

thdxr avatar Jul 16 '25 18:07 thdxr

I am not attempted to be super familiar with the code base, but just a quick search brings me to this:

https://github.com/sst/opencode/blob/dev/packages/opencode/src/util/timeout.ts

Is there a way to set this project to wait a certain time length max or indefinitely? Or does this have a default waiting period occuring at some stage of the project? Maybe some &http.Client method has a default timeout? I'm in a dr office waiting with my eyes dialated so can't really see anymore.. but maybe these notes help

mrbm avatar Jul 16 '25 21:07 mrbm

Maybe same as #655, opencode does not complete a download service but ends abruptly. My scenario is upgrading software packages in the Gentoo overlay. The download stops halfway, causing the Manifest that records the package to write an incorrect checksum.

douglarek avatar Jul 17 '25 03:07 douglarek

https://github.com/sst/opencode/blob/60b55f9d921805a4a9d38c9eed05a060df08df82/packages/opencode/src/tool/bash.ts

i see timeouts here, i have no idea how this might be effecting http.client requests to an llm or requests for other things, but i think there is a possible issue somehere.

mrbm avatar Jul 17 '25 04:07 mrbm

@thdxr i think the real pillar of strength that claude and google can't take from this project will be the ability to use local models which are obvious always going to run slower. I do think testing on a slow response scenario where from a separate machine running ollama might not response for 7 min while it thinks is a worthy test case to backtrack and find where open code might be closing the connection pre-maturely.

mrbm avatar Jul 17 '25 16:07 mrbm

yep will look into this

thdxr avatar Jul 24 '25 17:07 thdxr

I agree that timeout would be very convenient, but also for other capabilities like bash execution

noahfraiture avatar Jul 28 '25 18:07 noahfraiture

running into timeouts from slow local model responses as well. would be great to control.

ggaabe avatar Jul 31 '25 03:07 ggaabe

Same here when running a larger local model, would be nice if we could configure the timeout.

dc-tec avatar Aug 01 '25 06:08 dc-tec

same. My model was trying to do multiple tool calls and eventually timing out. I'd be happy to just be able to disable the timeout for local use

dysangel avatar Aug 03 '25 21:08 dysangel

Same here, getting constant timeouts with ollama. When this happens, the request is not aborted so the model keeps churning and subsequent requests are more likely to fail as the model is busy.

egyptianbman avatar Aug 09 '25 07:08 egyptianbman

I've been looking into this the last few days. By default a lot of network requests time out after 5 minutes. I'm submitting a PR to mlx-lm to send back SSE comments which notify that the prompt is currently processing and keep the connection alive. If necessary we could make similar keepalive tweaks to ollama and llama.cpp etc to allow time for local models to process very large contexts without all the clients disconnecting

dysangel avatar Aug 09 '25 07:08 dysangel

As I am experiencing the same timeout issue when running OpenCode against a local LMStudio installation, us local users need a way to override the default NodeJS timeout as documented here: https://blog.appsignal.com/2023/11/08/how-to-use-timeouts-in-nodejs.html

Hope this helps as I need it...

jamviking avatar Aug 09 '25 21:08 jamviking

@jamviking if you're using an MLX model, then yes that issue is still present

dysangel avatar Aug 09 '25 21:08 dysangel

@dysangel am actually using the GGUF version in LMStudio...

jamviking avatar Aug 10 '25 02:08 jamviking

Same here I'm using LMStudio with either Qwen3 coder or gpt-oss-20

and I get the timeoutError

Image

yokodev avatar Aug 10 '25 04:08 yokodev

Given that OpenCode uses the Vercel AI-SDK, could this be being limited by the maxDuration (https://vercel.com/docs/functions/limitations#max-duration)? My timeouts are all at about 5 minutes, which is the same as the default shown on the site.

TheHoodedFoot avatar Aug 10 '25 19:08 TheHoodedFoot

I have been able to replicate the timeout issue outside of OpenCode with a simple example that uses the AI SDK. It seems that the timeout is being caused because the Bun fetch implementation has a default timeout of 5 minutes, which affects the AI SDK. In order to get around this, it is neccessary to override fetch() in createOpenAICompatible(). The fetch timeout can be set to false, which waits indefinitely.

The following example will work on my slow machine running llama.cpp locally without timing out, but if the fetch is not overriden then it will fail with a timeout after 5 minutes, just like OpenCode:

import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { generateText } from 'ai';
import * as fs from 'fs';

const provider = createOpenAICompatible({
  name: 'provider-name',
  baseURL: 'http://127.0.0.1:5000/v1',
  fetch: async (input, init = {}) => {
    const fetchInit = {
      ...init,
      timeout: false,
    };
    return await fetch(input, fetchInit);
  },
});

const promptFromFile = fs.readFileSync('prompt.txt', { encoding: 'utf8' });
const { text } = await generateText({
  model: provider('qwen3-coder-30b-a3b-instruct'),
  prompt: promptFromFile,
});

process.stdout.write(text);

I assume that overriding the fetch function within OpenCode should have a similar effect.

TheHoodedFoot avatar Aug 15 '25 18:08 TheHoodedFoot

@TheHoodedFoot nice detective work on this timeout issue, hopefully @thdxr sees this( it is Friday and i assume he takes breaks). If he can implement an override of the fetch as you suggested and it works...then I am ready to test and maybe that will put my mac mini m4 back in business!

mrbm avatar Aug 15 '25 19:08 mrbm

I tested this and it seems to be working:

diff --git a/packages/opencode/src/provider/provider.ts b/packages/opencode/src/provider/provider.ts
index 42bb1804..5df59efd 100644
--- a/packages/opencode/src/provider/provider.ts
+++ b/packages/opencode/src/provider/provider.ts
@@ -305,9 +305,23 @@ export namespace Provider {
       const pkg = provider.npm ?? provider.id
       const mod = await import(await BunProc.install(pkg, "latest"))
       const fn = mod[Object.keys(mod).find((key) => key.startsWith("create"))!]
+      let options = { ...s.providers[provider.id]?.options }
+      // Inject custom fetch for local OpenAI-compatible providers
+      if (
+        provider.id === "ollama" &&
+        (provider.npm === "@ai-sdk/openai-compatible" || pkg === "@ai-sdk/openai-compatible") &&
+        options["baseURL"]
+      ) {
+        // Always override fetch to set timeout (default: false = unlimited, or user-configurable)
+        const userTimeout = options["timeout"] !== undefined ? options["timeout"] : false
+        options["fetch"] = async (input: any, init?: any) => {
+          return await fetch(input, { ...init, timeout: userTimeout })
+        }
+      }
       const loaded = fn({
         name: provider.id,
-        ...s.providers[provider.id]?.options,
+        ...options,
       })
       s.sdk.set(provider.id, loaded)
       return loaded as SDK

egyptianbman avatar Aug 15 '25 21:08 egyptianbman