Minideas

Idea 10 of 15 · The Claude API

Talking to Claude from code

Your code sends a list of messages and gets the next one back. Everything else is settings.

your requestmodel: …max_tokens: 1024messages: user: “Where is my order?”reply:“Which order number is it?”
1 / 5

A request is a list of messages

A request names the model, caps the length of the reply and carries the conversation so far as a list of messages; Claude returns the next message. It also carries your API key, so send it from your server, never from a visitor’s browser.

request 1yourequest 2youClaudeyourequest 3youClaudeyouClaudeyoueach request carries the whole chat so far
2 / 5

The API is stateless

Claude keeps nothing between requests. To continue a conversation, your code sends every earlier message again, plus the new one. It’s the “one long document” from “How a chatbot writes”, and here your code holds it.

system promptYou help customers of a small shop. Be brief.Never promise a refund: pass those to a person.Can I get a refund?I’ll pass you to our team.it shapes every reply in the conversation
3 / 5

The system prompt sets the role

A system prompt sits outside the conversation and shapes every reply: who Claude is here, who it’s talking to and what the rules are. For the shop’s assistant: be brief, and never promise a refund, pass those to a person.

temperature01ORD-418: shippedBloomORD-418: shippedNookORD-418: shippedHearthsteadyprecise workvariedcreative work
4 / 5

Temperature: steady or varied

Temperature sets how random each word choice is, as in “How a chatbot writes”. Near 0, a question gets nearly the same answer each time: good for precise work, like extracting data, classifying or writing code. Near 1, answers vary more: good for creative work, like naming, copywriting or brainstorming.

Your ordershipped on TueArrives Fri…{"order": 418,"status": "shipped"}streamingwords appear as they’re writtenstructured outputa fixed shape for your code
5 / 5

Streaming and a fixed shape

Long replies can stream: words arrive as they’re written, so nobody stares at a spinner. And when your code, not a person, reads the reply, use structured output to get JSON in an exact shape, so every answer can be read the same way.

Swipe the card, or use the arrow keys.

Go deeper

Each card stack gives you the idea. These open resources go into the detail.

Notes on the Claude Partner Network learning path on Anthropic Academy.