Calling LLM APIs
A chat window is only one way to use a language model. Most AI features in real products call the model from code, over HTTP, like any other web API. This page shows what goes into that request, what comes back, and what can go wrong.
6 min read6 মিনিটে পড়া যায় · 6 sections6টি সেকশন · 8 sources8টি সোর্স · updated Nov 2026আপডেট: Nov 2026
One request, one responseএকটা রিকুয়েস্ট, একটা রেসপন্স
Send a request, read the response, then switch streaming on and see the first text arrive sooner.একটা রিকুয়েস্ট পাঠান, রেসপন্স পড়ুন, তারপর স্ট্রিমিং চালু করে দেখুন প্রথম টেক্সট আগে আসে।
You’ll learnআপনি শিখবেন
- What goes in a requestরিকুয়েস্টে কী কী থাকে
- What a response holdsরেসপন্সে কী থাকে
- Why to check stop_reasonstop_reason কেন চেক করবেন
- What streaming changesস্ট্রিমিং কী বদলায়
In plain words
An LLM API is a web API that runs a large language model for you. Your code sends an HTTP request with the input, and the API sends back the text that the model wrote.
The Claude API is a REST API at https://api.anthropic.com Source 1সোর্স 1. Its main endpoint is POST /v1/messages. You send a list of input messages, and the model writes the next message of the conversation Source 2সোর্স 2. OpenAI and Google offer the same kind of API for their models Source 7সোর্স 7 Source 8সোর্স 8.
You can call the API with plain HTTP (for example curl) or with an official SDK. The SDK adds the required headers (the key, the API version, and the content type) for you Source 1সোর্স 1.
সহজ English
An LLM API is a service on the web. It runs a large language model for you. Your code sends the input. The API sends back the text that the model wrote.
The Claude API lives at https://api.anthropic.com Source 1সোর্স 1. Its main address is POST /v1/messages. You send a list of input messages. The model writes the next message of the conversation Source 2সোর্স 2. OpenAI and Google offer the same kind of API for their models Source 7সোর্স 7 Source 8সোর্স 8.
You can call the API with plain HTTP, for example with curl. You can also use an official SDK, which is a code library. The SDK adds the required headers for you Source 1সোর্স 1.
বাংলা ব্যাখ্যা
LLM API হলো একটা ওয়েব API, যা আপনার হয়ে একটা লার্জ ল্যাঙ্গুয়েজ মডেল চালায়। আপনার কোড ইনপুট সহ একটা রিকুয়েস্ট পাঠায়। API মডেলের লেখা টেক্সট ফেরত পাঠায়।
Claude API হলো https://api.anthropic.com-এ একটা REST API Source 1সোর্স 1। এর মূল এন্ডপয়েন্ট POST /v1/messages। আপনি ইনপুট মেসেজের একটা তালিকা পাঠান। মডেল কথোপকথনের পরের মেসেজটা লেখে Source 2সোর্স 2। OpenAI আর Google তাদের মডেলের জন্য একই ধরনের API দেয় Source 7সোর্স 7 Source 8সোর্স 8।
আপনি সাধারণ HTTP দিয়ে API কল করতে পারেন, যেমন curl দিয়ে। আবার একটা অফিশিয়াল SDK (কোড লাইব্রেরি) ব্যবহার করতে পারেন। SDK দরকারি হেডারগুলো আপনার হয়ে যোগ করে Source 1সোর্স 1।
How it works
The request
A request to the Claude API has these headers Source 1সোর্স 1:
| Header | Value |
|---|---|
Authorization | Bearer and your API key. The older x-api-key header is still supported as a fallback. |
anthropic-version | The API version |
content-type | application/json |
The body needs 3 fields Source 2সোর্স 2:
model: the model that writes the answer.max_tokens: the largest number of tokens the model may write. The model can stop before this limit, and each model has its own maximum.messages: the conversation so far. Each item has arole(userorassistant) andcontent. The roles take turns.
This is the basic request from the Anthropic docs. It uses the x-api-key header, and the key comes from an environment variable Source 3সোর্স 3:
curl https://api.anthropic.com/v1/messages \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2026-10-21" \
-H "content-type: application/json" \
-d '{
"model": "claude-opus-5-5",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "Hello, Claude"}]
}'With the Python SDK, the client reads ANTHROPIC_API_KEY by default Source 5সোর্স 5:
from anthropic import Anthropic
client = Anthropic()
message = client.messages.create(
model="claude-opus-5-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello, Claude"}],
)
for block in message.content:
if block.type == "text":
print(block.text)The response
The response is a JSON object Source 3সোর্স 3. These fields matter most Source 2সোর্স 2:
| Field | Meaning |
|---|---|
content | A list of content blocks. A text block has "type": "text". |
stop_reason | Why the model stopped: end_turn, max_tokens, stop_sequence, tool_use, pause_turn, refusal, or model_context_window_exceeded |
usage.input_tokens, usage.output_tokens | The tokens used by the request and by the answer |
Other providers
The idea is the same, but the names change. OpenAI's Responses API takes model and input, and the SDK gives an output_text property Source 7সোর্স 7. The Gemini API uses the header x-goog-api-key for the key Source 8সোর্স 8.
Streaming
If you set "stream": true, the API sends the answer in small parts with server-sent events (SSE), so the user sees text before the full answer is ready Source 6সোর্স 6.
সহজ English
A request to the Claude API has 3 headers Source 1সোর্স 1. Authorization holds Bearer and your API key. The older x-api-key header still works too. anthropic-version holds the API version. content-type is application/json.
The body needs 3 fields Source 2সোর্স 2:
model: the model that writes the answer.max_tokens: the largest number of tokens the model may write. The model can stop before this limit. Each model has its own maximum.messages: the conversation so far. Each item has arole(userorassistant) andcontent. The roles take turns.
The Anthropic docs show a basic curl request. The key comes from an environment variable, $ANTHROPIC_API_KEY Source 3সোর্স 3. With the Python SDK, the client reads ANTHROPIC_API_KEY by default Source 5সোর্স 5. The code examples are in the Technical text.
The response is a JSON object Source 3সোর্স 3. 3 fields matter most Source 2সোর্স 2:
contentis a list of content blocks. A text block has"type": "text".stop_reasonsays why the model stopped. For example,end_turnmeans a natural end.max_tokensmeans the limit was reached.refusalandmodel_context_window_exceededare other values.usagegives the tokens of the request and of the answer.
Other providers use the same idea with other names. OpenAI's Responses API takes model and input. Its SDK gives an output_text property Source 7সোর্স 7. The Gemini API takes the key in the header x-goog-api-key Source 8সোর্স 8.
If you set "stream": true, the API sends the answer in small parts (server-sent events). The user sees text before the full answer is ready Source 6সোর্স 6.
বাংলা ব্যাখ্যা
Claude API-তে একটা রিকুয়েস্টে ৩টা হেডার থাকে Source 1সোর্স 1। Authorization-এ থাকে Bearer আর আপনার API key। পুরোনো x-api-key হেডারও এখনো কাজ করে। anthropic-version-এ থাকে API ভার্সন। content-type হলো application/json।
বডিতে ৩টা ফিল্ড লাগে Source 2সোর্স 2:
model: যে মডেল উত্তর লিখবে।max_tokens: মডেল সর্বোচ্চ কতগুলো টোকেন লিখতে পারবে। মডেল এই সীমার আগেও থামতে পারে। প্রতিটা মডেলের নিজের সর্বোচ্চ মান আছে।messages: এখন পর্যন্ত কথোপকথন। প্রতিটা আইটেমে একটাrole(userবাassistant) আরcontentথাকে। রোলগুলো পালা করে আসে।
Anthropic ডকে একটা সাধারণ curl রিকুয়েস্ট আছে। Key আসে একটা এনভায়রনমেন্ট ভেরিয়েবল $ANTHROPIC_API_KEY থেকে Source 3সোর্স 3। Python SDK-তে ক্লায়েন্ট ডিফল্টভাবে ANTHROPIC_API_KEY পড়ে Source 5সোর্স 5। কোড উদাহরণগুলো Technical লেখায় আছে।
রেসপন্স হলো একটা JSON অবজেক্ট Source 3সোর্স 3। ৩টা ফিল্ড সবচেয়ে জরুরি Source 2সোর্স 2:
contentহলো কনটেন্ট ব্লকের একটা তালিকা। টেক্সট ব্লকে থাকে"type": "text"।stop_reasonবলে মডেল কেন থামল। যেমনend_turnমানে স্বাভাবিক শেষ।max_tokensমানে সীমায় পৌঁছেছে।refusalআরmodel_context_window_exceededঅন্য মান।usageরিকুয়েস্ট আর উত্তরের টোকেন সংখ্যা দেয়।
অন্য প্রোভাইডাররা একই ধারণা ভিন্ন নামে ব্যবহার করে। OpenAI-এর Responses API নেয় model আর input। এর SDK একটা output_text প্রপার্টি দেয় Source 7সোর্স 7। Gemini API key নেয় x-goog-api-key হেডারে Source 8সোর্স 8।
"stream": true দিলে API উত্তরটা ছোট ছোট অংশে পাঠায় (server-sent events)। পুরো উত্তর তৈরি হওয়ার আগেই ইউজার টেক্সট দেখতে পান Source 6সোর্স 6।
A real-life example
An HR team adds a "Summarize this policy" button to its intranet. The backend reads the policy text and sends one request to POST /v1/messages with a short instruction and the policy in the user message Source 2সোর্স 2.
In the first week, some summaries end in the middle of a sentence. The logs show stop_reason: "max_tokens" for those answers Source 2সোর্স 2. The team had set max_tokens too low for long policies. They raise the limit, and the app now checks stop_reason and shows a "summary is cut short" note when it is not end_turn.
On a busy morning, some requests fail with HTTP 529 (overloaded_error) and 429 (rate_limit_error) Source 4সোর্স 4. The Python SDK already retries these up to 2 times, waiting as long as the retry-after header says or using a short exponential backoff Source 5সোর্স 5. The team logs the request-id header of each failed call, so they can give it to support Source 4সোর্স 4.
সহজ English
An HR team adds a "Summarize this policy" button to its intranet. The backend reads the policy text. It sends one request to POST /v1/messages, with a short instruction and the policy in the user message Source 2সোর্স 2.
In the first week, some summaries stop in the middle of a sentence. The logs show stop_reason: "max_tokens" for those answers Source 2সোর্স 2. The team had set max_tokens too low for long policies. They raise the limit. The app now checks stop_reason. If it is not end_turn, the app shows a "summary is cut short" note.
On a busy morning, some requests fail. They get HTTP 529 (overloaded_error) and 429 (rate_limit_error) Source 4সোর্স 4. The Python SDK already retries these up to 2 times. It waits as long as the retry-after header says, or it uses a short growing wait between tries Source 5সোর্স 5. The team logs the request-id header of each failed call. They can give it to support Source 4সোর্স 4.
বাংলা ব্যাখ্যা
একটা HR টিম তাদের ইন্ট্রানেটে "Summarize this policy" বাটন যোগ করে। ব্যাকএন্ড পলিসির টেক্সট পড়ে। এটা POST /v1/messages-এ একটা রিকুয়েস্ট পাঠায়, যার ইউজার মেসেজে থাকে একটা ছোট নির্দেশনা আর পলিসিটা Source 2সোর্স 2।
প্রথম সপ্তাহে কিছু সারাংশ বাক্যের মাঝখানে থেমে যায়। লগে সেই উত্তরগুলোর জন্য stop_reason: "max_tokens" দেখা যায় Source 2সোর্স 2। লম্বা পলিসির জন্য টিম max_tokens খুব কম দিয়েছিল। তারা সীমা বাড়ায়। অ্যাপ এখন stop_reason চেক করে। এটা end_turn না হলে অ্যাপ একটা "সারাংশ ছোট হয়ে গেছে" নোট দেখায়।
এক ব্যস্ত সকালে কিছু রিকুয়েস্ট ফেল করে। সেগুলো পায় HTTP 529 (overloaded_error) আর 429 (rate_limit_error) Source 4সোর্স 4। Python SDK এগুলো আগে থেকেই সর্বোচ্চ ২ বার রিট্রাই করে; retry-after হেডার যতক্ষণ অপেক্ষা করতে বলে ততক্ষণ অপেক্ষা করে, নইলে অল্প বাড়ন্ত অপেক্ষা ব্যবহার করে Source 5সোর্স 5। টিম প্রতিটা ফেল করা কলের request-id হেডার লগ করে। তারা এটা সাপোর্টকে দিতে পারে Source 4সোর্স 4।
Common mistakes
- A key in the code. Keep the API key out of source control. The SDK docs suggest a
.envfile or an environment variable Source 5সোর্স 5. - Reading only the first block. The
contentfield is a list, and it can hold more than one block Source 2সোর্স 2. OpenAI warns that its output list often has more than one item, sooutput[0]is not always the text Source 7সোর্স 7. - Ignoring
stop_reason.max_tokensmeans that the answer is cut off, andrefusalmeans that the model refused Source 2সোর্স 2. Code that shows the text without this check shows a broken answer. - No error handling. Errors come back as JSON with a
typeand amessageSource 4সোর্স 4. A 401 means a problem with the key, 413 means the request is too large, and 500 means an internal error Source 4সোর্স 4. A streamed answer can also fail after the API sent a 200 Source 4সোর্স 4. - No timeout. The Python SDK waits 10 minutes by default before a request times out Source 5সোর্স 5. A user-facing feature usually needs a shorter limit.
সহজ English
- A key in the code. Keep the API key out of source control. The SDK docs suggest a
.envfile or an environment variable Source 5সোর্স 5. - Reading only the first block. The
contentfield is a list. It can hold more than one block Source 2সোর্স 2. OpenAI warns that its output list often has more than one item. Sooutput[0]is not always the text Source 7সোর্স 7. - Ignoring
stop_reason.max_tokensmeans the answer is cut off.refusalmeans the model refused Source 2সোর্স 2. Without this check, the app can show a broken answer. - No error handling. Errors come back as JSON with a
typeand amessageSource 4সোর্স 4. A 401 means a problem with the key. A 413 means the request is too large. A 500 means an internal error Source 4সোর্স 4. A streamed answer can also fail after the API sent a 200 Source 4সোর্স 4. - No timeout. The Python SDK waits 10 minutes by default before a request times out Source 5সোর্স 5. A feature for users usually needs a shorter limit.
বাংলা ব্যাখ্যা
- কোডে key রাখা। API key সোর্স কন্ট্রোলের বাইরে রাখুন। SDK ডক একটা
.envফাইল বা এনভায়রনমেন্ট ভেরিয়েবলের কথা বলে Source 5সোর্স 5। - শুধু প্রথম ব্লক পড়া।
contentফিল্ড একটা তালিকা। এতে একাধিক ব্লক থাকতে পারে Source 2সোর্স 2। OpenAI সতর্ক করে যে তার আউটপুট তালিকায় প্রায়ই একাধিক আইটেম থাকে। তাইoutput[0]সবসময় টেক্সট নয় Source 7সোর্স 7। stop_reasonউপেক্ষা করা।max_tokensমানে উত্তর কেটে গেছে।refusalমানে মডেল প্রত্যাখ্যান করেছে Source 2সোর্স 2। এই চেক ছাড়া অ্যাপ একটা ভাঙা উত্তর দেখাতে পারে।- কোনো এরর হ্যান্ডলিং নেই। এরর আসে
typeআরmessageসহ JSON হিসেবে Source 4সোর্স 4। 401 মানে key-এর সমস্যা। 413 মানে রিকুয়েস্ট খুব বড়। 500 মানে ভেতরের এরর Source 4সোর্স 4। স্ট্রিম করা উত্তর API 200 পাঠানোর পরেও ফেল করতে পারে Source 4সোর্স 4। - কোনো টাইমআউট নেই। Python SDK ডিফল্টভাবে টাইমআউটের আগে ১০ মিনিট অপেক্ষা করে Source 5সোর্স 5। ইউজারের জন্য একটা ফিচারে সাধারণত আরও ছোট সীমা লাগে।
What a tester can check
Tutorial advice
These test ideas are the tutorial's own advice.
- Mock the API in unit tests. Return fixed JSON with each
stop_reason, and check what the app shows for each one. - Force the errors. Use a wrong key (401), a huge input (413), and a mock 429 or 529. The app must show a clear message, not a stack trace.
- Cut the answer short. Set a very small
max_tokensin a test build. The app must not show a cut answer as complete. - Log the request ID. Check that each failed call writes its
request-idto the logs. - Search for keys. Scan the repo, the logs, and the browser bundle for API keys. A key must never reach the client.
- Watch the cost. Record
usagetokens per test run, so a prompt change that doubles the tokens is visible.
সহজ English
These test ideas are the tutorial's own advice.
- Mock the API in unit tests. Return fixed JSON with each
stop_reason. Check what the app shows for each one. - Force the errors. Use a wrong key (401), a huge input (413), and a mock 429 or 529. The app must show a clear message, not a stack trace.
- Cut the answer short. Set a very small
max_tokensin a test build. The app must not show a cut answer as complete. - Log the request ID. Check that each failed call writes its
request-idto the logs. - Search for keys. Scan the repo, the logs, and the browser bundle for API keys. A key must never reach the browser.
- Watch the cost. Record the tokens of each test run. Then a prompt change that doubles the tokens is easy to see.
বাংলা ব্যাখ্যা
এই টেস্ট আইডিয়াগুলো টিউটোরিয়ালের নিজের পরামর্শ।
- ইউনিট টেস্টে API মক (mock) করুন। প্রতিটা
stop_reasonসহ নির্দিষ্ট JSON ফেরত দিন। প্রতিটার জন্য অ্যাপ কী দেখায় তা চেক করুন। - এরর ঘটান। ভুল key (401), বিশাল ইনপুট (413), আর মক 429 বা 529 ব্যবহার করুন। অ্যাপকে স্ট্যাক ট্রেস নয়, একটা পরিষ্কার মেসেজ দেখাতে হবে।
- উত্তর ছোট করুন। টেস্ট বিল্ডে খুব ছোট
max_tokensদিন। অ্যাপ কাটা উত্তরকে সম্পূর্ণ হিসেবে দেখাবে না। - রিকুয়েস্ট ID লগ করুন। প্রতিটা ফেল করা কল তার
request-idলগে লেখে কিনা চেক করুন। - Key খুঁজুন। রিপো, লগ আর ব্রাউজার বান্ডেলে API key খুঁজুন। কোনো key কখনো ব্রাউজারে পৌঁছাবে না।
- খরচ দেখুন। প্রতিটা টেস্ট রানের টোকেন রেকর্ড করুন। তাহলে যে প্রম্পট বদল টোকেন দ্বিগুণ করে, তা সহজে দেখা যায়।
Key words
| Term | Meaning |
|---|---|
| LLM API | A web API that runs a language model, for example POST /v1/messages Source 2সোর্স 2 |
| API key | The secret that authenticates your requests Source 1সোর্স 1 |
| SDK | An official library that sends the headers and handles errors for you Source 1সোর্স 1 |
max_tokens | The largest number of tokens the model may write Source 2সোর্স 2 |
stop_reason | The reason why the model stopped, such as end_turn or max_tokens Source 2সোর্স 2 |
| Token | The unit of text that the API counts for input and output (usage) Source 2সোর্স 2 |
| Streaming | Getting the answer in parts with server-sent events Source 6সোর্স 6 |
| Request ID | A unique ID in the request-id header of each response Source 4সোর্স 4 |