Skip to contentমূল লেখায় যান

Topic 02 · AI engineeringটপিক 02 · AI engineering

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.একটা রিকুয়েস্ট পাঠান, রেসপন্স পড়ুন, তারপর স্ট্রিমিং চালু করে দেখুন প্রথম টেক্সট আগে আসে।

2 min2 মিনিট8 steps8টি ধাপ
  • Input tokensইনপুট টোকেন25
  • max_tokensmax_tokens (সীমা)1024
your appআপনার অ্যাপmodelmodel (মডেল)max_tokens: 1024max_tokens: 1024messages: [user]messages: [user]LLM APILLM API/v1/messages/v1/messages

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:

HeaderValue
AuthorizationBearer and your API key. The older x-api-key header is still supported as a fallback.
anthropic-versionThe API version
content-typeapplication/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 a role (user or assistant) and content. 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:

FieldMeaning
contentA list of content blocks. A text block has "type": "text".
stop_reasonWhy the model stopped: end_turn, max_tokens, stop_sequence, tool_use, pause_turn, refusal, or model_context_window_exceeded
usage.input_tokens, usage.output_tokensThe 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 a role (user or assistant) and content. 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:

  • content is a list of content blocks. A text block has "type": "text".
  • stop_reason says why the model stopped. For example, end_turn means a natural end. max_tokens means the limit was reached. refusal and model_context_window_exceeded are other values.
  • usage gives 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

সহজ English

বাংলা ব্যাখ্যা

  • কোডে 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_tokens in 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-id to 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 usage tokens 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_tokens in 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-id to 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

TermMeaning
LLM APIA web API that runs a language model, for example POST /v1/messages Source 2সোর্স 2
API keyThe secret that authenticates your requests Source 1সোর্স 1
SDKAn official library that sends the headers and handles errors for you Source 1সোর্স 1
max_tokensThe largest number of tokens the model may write Source 2সোর্স 2
stop_reasonThe reason why the model stopped, such as end_turn or max_tokens Source 2সোর্স 2
TokenThe unit of text that the API counts for input and output (usage) Source 2সোর্স 2
StreamingGetting the answer in parts with server-sent events Source 6সোর্স 6
Request IDA unique ID in the request-id header of each response Source 4সোর্স 4

Reels on this topicএই টপিকের রিলস

Short videos from other creators, picked for this topic. They are not part of this page's fact-checked text. Nothing loads from YouTube until you press Play.অন্য ক্রিয়েটরদের বানানো ছোট ভিডিও, এই টপিকের জন্য বাছাই করা। এগুলো এই পেজের ফ্যাক্ট-চেক করা লেখার অংশ নয়। চালান বাটন চাপার আগে YouTube থেকে কিছুই লোড হয় না।

  1. [1]
    API overview

    Anthropic (Claude Docs) · accessed 2026-10-28দেখা হয়েছে: 2026-10-28

  2. [2]
    Messages (API reference)

    Anthropic (Claude Docs) · accessed 2026-10-28দেখা হয়েছে: 2026-10-28

  3. [3]
    Using the Messages API

    Anthropic (Claude Docs) · accessed 2026-10-28দেখা হয়েছে: 2026-10-28

  4. [4]
    Claude API errors

    Anthropic (Claude Docs) · accessed 2026-10-28দেখা হয়েছে: 2026-10-28

  5. [5]
    Python SDK

    Anthropic (Claude Docs) · accessed 2026-10-28দেখা হয়েছে: 2026-10-28

  6. [6]
    Streaming Messages

    Anthropic (Claude Docs) · accessed 2026-10-28দেখা হয়েছে: 2026-10-28

  7. [7]
    Text generation

    OpenAI (API docs) · accessed 2026-10-28দেখা হয়েছে: 2026-10-28

  8. [8]
    Text generation

    Google AI for Developers (Gemini API docs) · accessed 2026-10-28দেখা হয়েছে: 2026-10-28