How to Call an LLM from a Chatbot (AWS Bedrock)

Connecting a chatbot to an LLM is one request: POST the user's message to the chat-completions endpoint, then pull the reply text out of the JSON. Here's the request shape, response parsing, and the error handling you need.

AI Engineerawsbedrockllm

The request shape

Most LLM chat APIs (AWS Bedrock's OpenAI-compatible endpoint, the OpenAI API, and local servers) take the same messages array and return the same choices shape:

import requests

LLM_URL = "https://<bedrock-endpoint>/v1/chat/completions"
MODEL = "anthropic.claude-3-haiku"

def chat(user_msg: str) -> str:
    r = requests.post(LLM_URL, json={
        "model": MODEL,
        "messages": [{"role": "user", "content": user_msg}],
    }, timeout=60)
    r.raise_for_status()
    return r.json()["choices"][0]["message"]["content"]

The reply text is at choices[0].message.content - that's the one path to remember.

Add a system prompt + history for a real chatbot

A single user message is stateless. A chatbot sends a system message (its persona/ rules) plus the running conversation history:

messages = [
    {"role": "system", "content": "You are a helpful support agent. Be concise."},
    *history,                                   # prior user/assistant turns
    {"role": "user", "content": user_msg},
]

Append each user message and the model's reply back into history so the next turn has context.

Error handling that matters

Streaming (optional)

For a typing-effect UI, request "stream": true and read the response incrementally (server-sent events) instead of waiting for the whole reply - the API returns token chunks you forward to the client as they arrive.

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

How do I call an LLM API from my app?

POST a JSON body with model and a messages array to the chat-completions endpoint, then read the reply at choices[0].message.content. AWS Bedrock's OpenAI-compatible endpoint, OpenAI, and local servers all use this shape.

How do I give a chatbot memory of the conversation?

Send a system message plus the running history of prior user/assistant turns in the messages array on every request, and append each new turn (and the model's reply) back into that history.

What error handling does an LLM call need?

Set a timeout (calls are slow), check the status (back off on 429/5xx), and guard the response parsing so a malformed or empty reply doesn't crash your handler.

Keep learning

Restore a Broken LLM API IntegrationAI/ML projectHarden an LLM Pipeline Against API FailuresAI/ML projectParse JSON From an LLM (Strip Markdown Fences)AI/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 →