Artificial Intelligence

How to Use an LLM API: A Beginner's Walkthrough

A plain-English walkthrough from your first LLM API call to production basics: keys, parameters, streaming, error handling, rate limits and keeping costs under control.

Quick answer

To use an LLM API, create an account with a provider, generate an API key and store it in an environment variable, install the provider's SDK, then send a request with a model name and your messages. Before going live, add streaming, retries with exponential backoff, a server-side proxy for your key, spend limits and token logging.

How to Use an LLM API: A Beginner's Walkthrough

Photo: Unsplash

On this page

Chat apps are great for trying AI. But the moment you want AI inside your own product, spreadsheet, help desk or internal tool, you need the API. That’s where many beginners stall: the docs assume you already know what a token is, why your request failed with a 429, and why your bill doubled overnight.

This walkthrough covers the path from your first API call to a setup you can trust in production. The examples use the patterns shared by the major providers, so the concepts carry over whether you pick OpenAI, Anthropic, Google or an open-model host.

What an LLM API is and how it works

An LLM API lets your code send text (and often images or files) to a large language model over the internet and get a response back. If APIs in general are new to you, start with our explainer on what an API is.

Every request has the same basic shape:

  • An endpoint: the URL you send the request to.
  • Authentication: your secret API key, sent in a request header.
  • A model name: which model should answer.
  • Messages: the conversation so far, usually with roles such as system or developer (your instructions), user (the input) and assistant (earlier model replies).
  • Parameters: settings such as the maximum length of the reply.

The response contains the generated text plus metadata, including how many input and output tokens you used. Tokens are what you pay for, so it’s worth reading our guide to AI tokens and context windows before you build anything large.

One more thing that surprises beginners: the API is stateless. The model does not remember your last request. If you’re building a chat, your code has to send the earlier messages again each time.

Steps 1 to 3: pick a model, get a key and make your first call

Step 1: Choose a provider and model

Your choice depends on quality, price, speed, data handling terms and where you already run your infrastructure. Our comparison of ChatGPT vs Claude vs Gemini covers the consumer side; for open-weight options, weigh the trade-offs between open and closed models.

Option Examples Good for Watch out for
Model maker’s own API OpenAI, Anthropic, Google Newest models and features first Separate account, billing and terms per provider
Cloud platform Amazon Bedrock, Google Cloud Vertex AI, Microsoft Azure Teams already on that cloud, enterprise controls Feature timing and pricing can differ from the direct API
Open-model host Various inference providers Running open-weight models without your own GPUs Quality and reliability vary by host
Self-hosted Your own servers Strict data control You manage hardware, scaling and updates

Most providers offer a family of models: a large, more capable and more expensive model, and smaller, faster and cheaper ones. Start testing with a mid-tier model, then try the smaller one once your prompts work.

Step 2: Get an API key and keep it secret

Sign up in the provider’s developer console, add billing, and create an API key. Then treat it like a password.

  • Store it in an environment variable, not in your code. The official SDKs read standard variables such as OPENAI_API_KEY or ANTHROPIC_API_KEY automatically.
  • Never put the key in browser or mobile app code. OpenAI’s key safety guidance warns that a key exposed client-side lets anyone make requests on your account. Route requests through your own backend instead.
  • Never commit it to a code repository, even a private one. Use a secrets manager in production.
  • Use separate keys for development and production, and rotate them on a schedule.
  • Set a spend limit in the console so a bug can’t run up an unlimited bill.

Step 3: Make your first call

Install the provider’s SDK (for example, pip install openai or pip install anthropic for Python) and send a short request. Here’s the shape of a minimal call with Anthropic’s Python SDK:

```python import anthropic

client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY

message = client.messages.create( model=”MODEL_NAME_FROM_DOCS”, max_tokens=500, system=”You are a concise assistant for a plumbing company.”, messages=[{“role”: “user”, “content”: “Write a 2-sentence reminder for a Tuesday appointment.”}], ) print(message.content[0].text) ```

OpenAI’s quickstart uses the same idea with its Responses API: create a client, call client.responses.create() with a model and an input, and print the output text. Model names change often, so copy the current one from your provider’s model list rather than from a tutorial.

If the call works, you’ll see the reply printed. If not, the error message will usually tell you what’s wrong: a missing key, a typo in the model name or a malformed request.

Step 4: Learn the parameters that matter

You don’t need every setting. These are the ones beginners use most:

  • Max tokens / max output tokens. The cap on reply length. Anthropic’s Messages API requires max_tokens. OpenAI notes that your rate limit usage is based on the larger of this value and your estimated input, so set it close to what you actually need.
  • System or developer instructions. The rules, tone and role for the model. Keep them stable and put changing input in the user message.
  • Temperature. On models that support it, lower values make output more predictable and higher values more varied. Reasoning models may ignore it or use other controls instead.
  • Stop sequences. Text that tells the model to stop generating.
  • Structured output. Many APIs can return JSON that matches a schema you define. Use this whenever your code needs to parse the response.
  • Tools. You can describe functions the model may ask your code to run, such as looking up an order. This is the foundation of agents and of standards like Model Context Protocol.

Step 5: Add streaming for a better user experience

Without streaming, your user stares at a spinner until the full reply is ready. With streaming, text appears as it’s generated. Providers deliver streams using server-sent events (SSE), and the SDKs wrap this in simple loops.

Use streaming for chat interfaces and any long response. Anthropic also recommends streaming or its batch API for long-running requests, because some networks drop idle connections. One gotcha from Anthropic’s docs: with streaming, an error can arrive mid-stream after the request has already returned a 200 status, so your code needs to handle error events inside the stream too.

Key takeaway: A first API call takes minutes. What makes it production-ready is the boring part: secure keys, retries, streaming, logging and spend limits. Build those in before real users arrive.

Step 6: Handle errors and rate limits

Every provider limits how much you can send. OpenAI measures limits in requests per minute and per day, and tokens per minute and per day, and you hit whichever comes first. Limits usually rise as your account moves up usage tiers.

Common errors and what to do:

Status Meaning What to do
400 Bad request (wrong format or parameter) Fix the request; don’t retry as-is
401 Invalid or missing API key Check the key and environment variable
403 Key lacks permission Check workspace or project settings
429 Rate limit or quota reached Retry with exponential backoff, or wait for quota
500 Provider-side error Retry with backoff
529 (Anthropic) API temporarily overloaded Retry with backoff

Best practices from the providers’ docs:

  1. Retry with exponential backoff and jitter. Wait a little, then longer, with a random delay so all your workers don’t retry at once. If a retry-after header is present, respect it.
  2. Cap retries by number and total time.
  3. Don’t retry errors that need action, such as billing or quota problems.
  4. Log request IDs. Anthropic returns a request-id header on every response; include it when you contact support.

The official SDKs already retry some transient errors for you. Anthropic’s SDK, for example, retries twice by default with exponential backoff.

Step 7: Control costs from day one

LLM APIs charge per token, with input and output priced separately and output usually costing more. On Anthropic’s published price list, for example, output tokens cost 5 times the input price for its current models. A few habits keep bills predictable:

  • Log tokens per request from the usage data in each response, and track cost per feature or customer.
  • Set hard spend limits and budget alerts in the console.
  • Trim prompts. Don’t resend huge documents or long chat histories when a summary will do.
  • Use smaller models for simple tasks like classification and extraction.
  • Use batch processing for work that isn’t urgent. OpenAI and Anthropic both offer a 50% discount through their batch APIs.
  • Use prompt caching when many requests share the same long instructions or documents.

Our guide on how to reduce LLM API costs goes deeper on each technique.

Where to start: a production checklist

Before you put an LLM feature in front of customers, check off these items:

  1. API key stored in a secrets manager and only used server-side
  2. Spend limit and budget alerts set in the provider console
  3. Streaming enabled for user-facing responses
  4. Retries with exponential backoff and a sensible timeout
  5. Logging of request IDs, token counts, latency and errors
  6. Input limits so users can’t send huge prompts
  7. A small test set to check quality whenever you change the prompt or model (see how to evaluate LLM outputs)
  8. A fallback message for when the API is down
  9. A review of the provider’s data use and retention terms
  10. A plan for adding your own data, for example with retrieval-augmented generation

Conclusion

Using an LLM API is easier than it looks: a key, a model name, some messages and a few parameters. The real work is making it reliable, secure and affordable. Start with a small script, get one useful result, then add streaming, retries, logging and cost controls before you scale. If this is the start of a career shift, our roadmap on how to become an AI engineer shows what to learn next.

Sources

Questions readers ask

Frequently asked questions

How much does it cost to use an LLM API?

LLM APIs charge per token, usually quoted per million tokens, with separate prices for input and output. Costs range from fractions of a cent to several dollars per million tokens depending on the model, and output usually costs more than input. Most providers let you add prepaid credit and set spend limits, so you can test for very little money.

Do I need to know how to code to use an LLM API?

You need basic programming skills, usually in Python or JavaScript, to call an API directly. If you don't code, no-code automation platforms such as Zapier or Make offer AI steps that call these APIs for you. For a single custom assistant, a configurable chatbot builder may be enough without touching the API.

Is it safe to send company data to an LLM API?

It can be, but check first. Read the provider's data use, retention and training terms for API traffic, which are often different from consumer chat apps. Use business agreements where needed, avoid sending data you don't need, and follow your company's AI policy. Regulated data, such as health records, may require specific contracts.

What is the difference between the ChatGPT app and the OpenAI API?

The ChatGPT app is a finished product with its own subscription and interface. The OpenAI API is a developer service you call from your own code and pay for per token. Billing is separate, so a ChatGPT subscription does not include API usage, and the API gives you control over prompts, parameters and integration.

Why does my LLM API return a 429 error?

A 429 means you've hit a rate limit, such as requests or tokens per minute, or reached a quota or spend cap. Retry with exponential backoff and respect any retry-after header. If it keeps happening, reduce request volume, lower max tokens, use batch processing for non-urgent jobs or ask your provider about higher usage tiers.

Should I use streaming with an LLM API?

Yes, for anything a person waits on, such as chat or long answers. Streaming shows text as it's generated, which makes your app feel much faster. It also helps avoid timeouts on long requests. For background jobs where nobody is watching, a standard request or a batch job is simpler.

Which LLM API is best for beginners?

There isn't one best choice. The major providers all have Python and JavaScript SDKs, quickstart guides and pay-as-you-go pricing. Pick the one whose models perform best on a small test of your real task, whose data terms fit your needs, and that works with the cloud you already use.

Reader reviews

Was this guide useful

No reviews yet. Share your experience and help other readers.

Write a review
Your rating
Never shown publicly

Reviews are checked before they appear. We never show your email. Please review only places or services you have used.

Manishdai
Written by

Manishdai

Manishdai writes about the technology that is reshaping every industry — from AI agents and cloud platforms to healthtech, fintech and the IT companies building it all across the United States.