Appendix B · section 5 of 32
ask/talk.ts
From Talking to a Model.
import Anthropic from "@anthropic-ai/sdk";
// A missing credential should be one clear line, not a stack trace.
if (!process.env.ANTHROPIC_API_KEY) {
console.error("ANTHROPIC_API_KEY is not set. Export it and run this again.");
process.exit(1);
}
const client = new Anthropic();
async function main(): Promise<void> {
const response = await client.messages.create({
model: "claude-opus-5-5",
max_tokens: 1024,
messages: [
{
role: "user",
content:
"In two sentences: how far from a fire hydrant may I park in Maryland?",
},
],
});
for (const block of response.content) {
if (block.type === "text") {
console.log(block.text);
}
}
console.log(
`\n[in: ${response.usage.input_tokens} out: ${response.usage.output_tokens}]`,
);
}
main().catch((error: unknown) => {
console.error(error instanceof Error ? error.message : String(error));
process.exit(1);
});