Skip to content

perf: LLM API Fails with "fetch failed" Error When Using Ollama #310

@akhileshthite

Description

@akhileshthite

When attempting to use local LLM services with Ollama, the browser throws a TypeError: fetch failed error.

This occurs because Node.js's built-in fetch (via undici) imposes default timeouts that are too restrictive for large language models, which can take significant time.

Error:

Error: Error invoking remote method 'lIm-chat': TypeError: fetch failed
Image

Potential solution:

import { Agent } from 'undici';

const llmAgent = new Agent({
  headersTimeout: 0, // No timeout for headers
  bodyTimeout: 0     // No timeout for body
});

// Applied to all fetch requests
const response = await fetch(url, {
  method: 'POST',
  headers,
  body: JSON.stringify(data),
  dispatcher: llmAgent // Use custom agent
});

This allows large models to take unlimited time to respond without hitting artificial timeout limits, enabling reliable local LLM functionality with Ollama.

Hardware:

  • M1 Mac 8 gb RAM

Metadata

Metadata

Assignees

No one assigned

    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