Your first AI call in 5 minutes
15 minSend your first message to Claude, understand the response, then build the Module 0 project, a chat that runs in your terminal.
Time to make Claude talk. In this lesson you'll send one message, read the answer, and understand every line of the code that did it. Then you'll turn it into a real chat program.
The smallest possible AI program
Create a file called hello.ts (or hello.py) in your project folder:
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic(); // reads ANTHROPIC_API_KEY automatically
const response = await client.messages.create({
model: "claude-opus-5-5",
max_tokens: 16000,
messages: [
{ role: "user", content: "Explain what an API is in one sentence, using a car analogy." },
],
});
for (const block of response.content) {
if (block.type === "text") console.log(block.text);
}
console.log(response.usage);import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY automatically
response = client.messages.create(
model="claude-opus-5-5",
max_tokens=16000,
messages=[
{"role": "user", "content": "Explain what an API is in one sentence, using a car analogy."},
],
)
for block in response.content:
if block.type == "text":
print(block.text)
print(response.usage)Run it:
npx tsx hello.ts # or: python3 hello.pyAfter a few seconds you'll see Claude's answer, followed by some numbers. Congratulations, you just built an AI program.
What each part does
client.messages.create(...) sends one request to Claude through the Messages API. Almost everything you build in this course goes through this one call.
model picks which Claude model answers. claude-opus-5-5 is Anthropic's current flagship. You'll learn how to choose between models in Module 1.
max_tokens is a ceiling on how long the answer can be, not a target. Claude stops when it's done, usually well before the limit. Set it too low and answers get cut off mid-sentence, so be generous.
messages is the conversation so far: a list of turns, each with a role ("user" or "assistant") and content. Right now it's a single user message.
Reading the response
The response isn't a plain string. response.content is a list of blocks, and each block has a type.
Why? Because Claude can return more than text. This model thinks before answering, so the list can include a thinking block before the text block, and later in the course you'll see tool_use blocks too. That's why we loop and only print blocks where type is "text".
The usage object at the end shows how many tokens went in (input_tokens) and came out (output_tokens). Tokens are how AI usage is measured and billed. Module 1 is all about them.
Mini-project: Hello, Claude CLI
A single question is nice, but a conversation is better. Let's build a chat that runs in your terminal and remembers what you said.
The trick: Claude has no memory between requests. Each call is independent. To hold a conversation, you keep the history and send the whole thing every time.
import Anthropic from "@anthropic-ai/sdk";
import * as readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";
const client = new Anthropic();
const rl = readline.createInterface({ input, output });
// The conversation so far. We send all of it on every turn.
const messages: Anthropic.Beta.BetaMessageParam[] = [];
console.log('Chat with Claude. Type "exit" to quit.\n');
while (true) {
const userText = (await rl.question("you › ")).trim();
if (!userText) continue;
if (userText.toLowerCase() === "exit") break;
messages.push({ role: "user", content: userText });
try {
const response = await client.beta.messages.create({
model: "claude-opus-5-5",
max_tokens: 16000,
system: "You are a friendly, concise assistant. Keep answers short unless asked for detail.",
// Safety net: if this model declines a request, a fallback model answers instead.
betas: ["server-side-fallback-2026-07-01"],
fallbacks: "default",
messages,
});
if (response.stop_reason === "refusal") {
console.log("\nclaude › (declined to answer that one)\n");
messages.pop(); // drop the declined message so the chat can continue
continue;
}
const reply = response.content
.map((block) => (block.type === "text" ? block.text : ""))
.join("");
console.log(`\nclaude › ${reply}\n`);
// Save Claude's full reply (all blocks, not just the text) into the history.
messages.push({ role: "assistant", content: response.content });
} catch (error) {
messages.pop(); // the request failed, so forget the unanswered message
if (error instanceof Anthropic.AuthenticationError) {
console.error("Your API key was rejected. Check ANTHROPIC_API_KEY.");
break;
} else if (error instanceof Anthropic.RateLimitError) {
console.error("Rate limited. Wait a moment and try again.");
} else if (error instanceof Anthropic.APIError) {
console.error(`API error ${error.status}: ${error.message}`);
} else {
throw error;
}
}
}
rl.close();import anthropic
client = anthropic.Anthropic()
messages = [] # the conversation so far; we send all of it on every turn
print('Chat with Claude. Type "exit" to quit.\n')
while True:
user_text = input("you › ").strip()
if not user_text:
continue
if user_text.lower() == "exit":
break
messages.append({"role": "user", "content": user_text})
try:
response = client.beta.messages.create(
model="claude-opus-5-5",
max_tokens=16000,
system="You are a friendly, concise assistant. Keep answers short unless asked for detail.",
# Safety net: if this model declines a request, a fallback model answers instead.
betas=["server-side-fallback-2026-07-01"],
fallbacks="default",
messages=messages,
)
except anthropic.AuthenticationError:
print("Your API key was rejected. Check ANTHROPIC_API_KEY.")
break
except anthropic.RateLimitError:
messages.pop()
print("Rate limited. Wait a moment and try again.")
continue
except anthropic.APIStatusError as e:
messages.pop()
print(f"API error {e.status_code}: {e.message}")
continue
if response.stop_reason == "refusal":
print("\nclaude › (declined to answer that one)\n")
messages.pop()
continue
reply = "".join(block.text for block in response.content if block.type == "text")
print(f"\nclaude › {reply}\n")
# Save Claude's full reply (all blocks, not just the text) into the history.
messages.append({"role": "assistant", "content": response.content})Run it with npx tsx chat.ts (or python3 chat.py), then try this: tell it your name, ask something else, then ask "What's my name?" It remembers, because you're sending the full history each time.
Three things worth noticing
systemsets Claude's behaviour for the whole conversation: its role, tone and rules. It's the most powerful lever you have, and Module 2 goes deep on it.- We save
response.content, not just the text. Claude's reply can include blocks other than text (like its thinking), and sending them back unchanged keeps the conversation working correctly. - Errors are handled by type. A bad key, a rate limit and other API errors each get a clear message instead of a crash. Real apps always do this.
Try it: make it yours
- Change the
systemprompt to give Claude a personality. Try "You are a grumpy mechanic who explains everything using car analogies." - Print the token usage after each reply:
response.usage.input_tokensandresponse.usage.output_tokens. Chat for a few turns. What happens toinput_tokensas the conversation gets longer, and why? (Hint: what are you sending every turn?) - Bonus: add a
/resetcommand that clears the history.
That last question about input tokens is exactly where Module 1 begins.