How to Fix a Broken LLM API Integration

When an LLM call fails with a connection error or 'model not found', the bug is usually in how you're calling it - wrong URL/path, or a missing required field like model. Here's how to debug and fix it.

AI Engineerpythonllmapi-integration

The two failures, and what they mean

Read the exact error first - it tells you which of the two you have.

The correct request

Modern chat models use /v1/chat/completions with a model and a messages array:

import requests

LLM_URL = "http://llm-service:8080/v1/chat/completions"   # right host:port + path

def summarize(text: str) -> str:
    payload = {
        "model": "the-model-name",                 # REQUIRED - omitting it = "model not found"
        "messages": [{"role": "user", "content": f"Summarize:\n{text}"}],
    }
    r = requests.post(LLM_URL, json=payload, timeout=60)
    r.raise_for_status()
    return r.json()["choices"][0]["message"]["content"]

Debug it methodically

  1. Curl the endpoint to separate network from payload: bash curl -sS http://llm-service:8080/v1/chat/completions \ -H 'Content-Type: application/json' \ -d '{"model":"the-model-name","messages":[{"role":"user","content":"hi"}]}' - Connection refused -> wrong host/port. 404 -> wrong path. 400/"model" -> body issue.
  2. Check the path - chat models are /v1/chat/completions; the older text route is /v1/completions (different body shape).
  3. Check required fields - model is required; messages (chat) vs prompt (completions) must match the route.
  4. Parse the right field - the reply is at choices[0].message.content for chat.

Make it robust

Want to try it hands-on? HeyDevJob gives you this exact setup in a live cloud workspace in your browser - edit it, run it, and see it work. Free, nothing to install.

Try it in a workspace →

What you'll practice

FAQ

Why does my LLM API call return 'model not found'?

The request reached the API but the body is wrong - usually a missing or incorrect model field, or you're posting to the wrong route (/v1/completions vs /v1/chat/completions). Add the correct model and match the route to the body shape.

Why do I get a connection error calling an LLM API?

Wrong host, port, or path - the request never reaches a working endpoint. Curl the URL directly: connection refused means wrong host/port, a 404 means wrong path.

Where is the reply in an LLM chat completions response?

At choices[0].message.content. (For the older /v1/completions route it's choices[0].text.)

Why is my LLM API call failing?

Most LLM API failures are a wrong endpoint URL or path, or a missing or incorrect model field in the request body. Check the exact URL, confirm the model name, and read the error response - it usually names the problem.

Keep learning

Harden an LLM Pipeline Against API FailuresAI/ML projectParse JSON From an LLM (Strip Markdown Fences)AI/ML projectBuild a Text Summarization Endpoint With an LLMAI/ML projectAI/ML roadmapStep by step to hiredAI/ML interview questionsSTAR answersAll AI/ML projectsProjects hub

Learn it by doing. Open this in a live cloud workspace, make the change yourself, and keep a record of the work you can share.

Open the workspace →