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.
The two failures, and what they mean
- Connection error / timeout -> you're hitting the wrong host, port, or path. The request never reaches a working endpoint.
- "model not found" / 400 / 404 from the API -> the request arrived but the body is
wrong - usually a missing or wrong
modelfield, or the wrong route (/v1/completionsvs/v1/chat/completions).
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
- 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. - Check the path - chat models are
/v1/chat/completions; the older text route is/v1/completions(different body shape). - Check required fields -
modelis required;messages(chat) vsprompt(completions) must match the route. - Parse the right field - the reply is at
choices[0].message.contentfor chat.
Make it robust
- Always set a timeout - LLMs are slow and a hang shouldn't freeze your app.
raise_for_status()+ retry on 429/5xx with backoff.- Guard the response parse so a malformed reply doesn't crash the caller.
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
- Curling the endpoint to isolate network vs payload
- Sending the required model + messages for the right route
- Adding timeout, retry, and safe response parsing
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
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 →