# LLM API

> The LLM API is an interface that allows applications to send requests to a language model and receive responses.

## How do I call an LLM API from my app?

The HTTP interface provided by model vendors to send messages and receive generated responses. Usually supports streaming, tool calling and structured output, and is billed by the number of input and output tokens.

- Topic: APIs & Integrations
- Type: technology

## Example

Wrong:

```js
const res = await fetch(LLM_API_URL, {
  method: 'POST',
  headers: { Authorization: 'Bearer ' + 'my-secret-key' },
  body: JSON.stringify({ messages }),
});
```

Right:

```js
const res = await fetch(process.env.LLM_API_URL, {
  method: 'POST',
  headers: { Authorization: 'Bearer ' + process.env.LLM_API_KEY },
  body: JSON.stringify({ messages, max_tokens: 1024 }),
  signal: AbortSignal.timeout(60_000),
});
```

The call runs on the server with the key from the environment, an output token limit and a timeout.

## Common mistakes

- Putting the API key in frontend code where anyone can read it.
- No retry logic, so rate limits break the app.

## Key points

- The API key stays on the server and is never sent to the browser.
- Requests have a timeout and retry with backoff on rate-limit and server errors.
- A maximum output token limit is set on each request.
- Responses are streamed when a user is waiting for long output.

## Interview questions

### Can I call an LLM API directly from the browser?

Not with your secret key. Send requests to your own server, which adds the key, or use short-lived tokens where the provider supports them.

### Why are LLM API calls billed by tokens?

Cost scales with the text the model reads and writes. Input and output tokens are counted separately, and output tokens usually cost more.

### What is the difference between system and user messages?

The system message sets instructions and behavior for the whole conversation. User messages contain the requests the model answers.

## Related cards

- [Rate Limits](https://wonderium.dev/ai/rate-limits.md)
- [Streaming](https://wonderium.dev/ai/streaming.md)
- [Max Tokens](https://wonderium.dev/ai/max-tokens.md)
- [Token Cost](https://wonderium.dev/ai/token-cost.md)
- [Message Roles](https://wonderium.dev/ai/message-roles.md)

---

- Level: [Artificial Intelligence](https://wonderium.dev/ai.md)
- HTML version: https://wonderium.dev/ai/llm-api
- Last updated: 2026-11-25
