Careeroza
Your First Request
messages.create
Three things are required: model, max_tokens (upper limit for the reply) and messages (the conversation, starting with a user message).
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-5-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Explain what an API is to a college student in 2 sentences."}],
)
for block in response.content: # a list of content blocks
if block.type == "text":
print(block.text)
print("---")
print("model:", response.model)
print("stop_reason:", response.stop_reason)
print("input tokens:", response.usage.input_tokens, "| output tokens:", response.usage.output_tokens)Run it
python first_call.pyExample output
An API (Application Programming Interface) is a set of rules that lets one program ask another program for data or actions, like a waiter taking your order to the kitchen and bringing back food. For example, a weather app uses a weather service's API to fetch today's forecast instead of measuring it itself. --- model: claude-opus-5-5 stop_reason: end_turn input tokens: 22 | output tokens: 71
Reading the Response
Example
The response is JSON. Here is a sample response (as a Python dict) and the code to read it. content is a list because a reply can contain several blocks (text, thinking, tool calls), so always check the block type:
response = {
"id": "msg_01XFDUDYJgAACzvnptvVoYEL",
"type": "message",
"role": "assistant",
"model": "claude-opus-5-5",
"content": [
{"type": "thinking", "thinking": ""},
{"type": "text", "text": "Hello! How can I help you today?"},
],
"stop_reason": "end_turn",
"usage": {"input_tokens": 10, "output_tokens": 12},
}
text = "".join(b["text"] for b in response["content"] if b["type"] == "text")
print("reply:", text)
print("finished normally:", response["stop_reason"] == "end_turn")
u = response["usage"]
cost = u["input_tokens"] * 4 / 1e6 + u["output_tokens"] * 20 / 1e6
print(f"tokens: {u['input_tokens']} in, {u['output_tokens']} out -> USD {cost:.6f}")Output
reply: Hello! How can I help you today? finished normally: True tokens: 10 in, 12 out -> USD 0.000280
Note: Current Claude Opus models always think before answering, so a thinking block can appear before the text. Code that does response.content[0].text can break; loop and pick type == "text".