Joel Stevick

Chapter 1 · section 2 of 9

Your first call

Create a directory called ask inside your project. Everything you build in the chapters ahead lives there.

One file, about forty lines, no framework of any kind. Type it.

Listing: ask/talk.ts

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);
});

Run it:

npx tsx ask/talk.ts

You should get two confident sentences about Maryland hydrants, with a distance in them, and a line of counts. If you got a line about ANTHROPIC_API_KEY, export the key as described in the introduction and try again.

Read the two sentences again. The distance is probably 15 feet, which is right. Run it four more times and it may say 15 every time, or it may say 10 once, which is Ohio’s number, or 20, which is Nevada’s where angle parking is allowed. Nothing in the reply tells you which run you are looking at. Each one is written in the same voice, with the same assurance, and the one with the wrong number is not shorter or more hesitant than the others.

The problem is all there in those five runs, and it is not a defect in this model that a better one will remove. A program that forwards those two sentences to a sixteen-year-old is a program that will sometimes send the wrong distance with nothing to mark it. Everything after this chapter is about building a program that does not.

Before that, go through the request slowly, because five of those lines are concepts every later chapter uses.