The client in my series is one fetch to the Messages API, no SDK, in a file called claude.ts, the only place the repository talks to the model. Its first question, what HTTP status 304 means, came back correct in 79 output tokens for $0.0008. Its second, how many days a customer has to return a lantern from a fictional company, came back as 323 tokens of advice and no answer, for $0.0033. Both were function calls: a string in, a string out, and a count of what it cost.

Free, step by step: the video series at https://www.youtube.com/@JoelStevick and the site, https://joelstevick.com, with the demo and the book.

Watch the episode: Calling a model from code

Watch the episode: Prompts are code

The request

export async function ask(messages: Message[], options: Options = {}): Promise<Answer> { const body = { model: MODEL, max_tokens: options.maxTokens ?? 1024, system: options.system, messages, // Hidden reasoning is billed as output. These are lookups, not proofs: keep it short. output_config: { effort: "low" }, }; const data = await post(API, body); return { text: data.content.filter((b) => b.type === "text").map((b) => b.text).join(""), stopReason: data.stop_reason, inputTokens: data.usage.input_tokens, outputTokens: data.usage.output_tokens, cost: cost(data.usage), }; } 

Five fields. model is a constant at the top of the file, claude-sonnet-5-5; changing models is changing one line. max_tokens is the longest the answer may be, in tokens, 1,024 by default. system is the system prompt: instructions that sit above the conversation and apply to every turn of it. messages is the conversation, each message with a role, user or assistant. The model keeps no state between calls, so this list is everything it knows about the conversation. And effort is low: the model can reason before it answers, that hidden reasoning is billed as output tokens, and for lookups it is kept short.

post is a fetch: method POST, a JSON body, and three headers, the content type, anthropic-version, which pins the API's behaviour to a dated version, and x-api-key, read from an environment variable. A response that is not OK throws with the status and the body.

The response

The response's content is a list of blocks, not a string; the client keeps the text blocks and joins them. stop_reason says why the answer ended: end_turn means the model finished, max_tokens means it hit the limit and was cut off. If your code parses what comes back, a cut-off answer is broken input, and stop_reason is how your code finds out. usage holds the token counts, input and output, as the server counted them, and the client turns them into dollars.

Tokens and cost

A token is a piece of text the model reads and writes as one unit, usually a few characters long. tokens.ts counts them through the count_tokens endpoint, which does not run the model and is free. One sentence from the series' documents, "The Firefly 220R charges in 3.5 hours over USB-C.", is 49 characters, 9 words, and 32 tokens. A short common word is one token; numbers, product names and punctuation split into several each. You cannot price a prompt by its word count. Count it.

The price list is one constant: $2 per million input tokens and $10 per million output tokens. cost multiplies each count by its price and divides by a million. Output is five times the price of input, so a short question with a long answer is mostly paying for the answer: the 304 question was 27 tokens in and 79 out, about a twelfth of a cent, nearly all of it the answer.

The context window is the most a model can take in one call, counted in tokens, prompt and answer together. Every request carries the whole conversation, so the input grows with every turn and you pay for the earlier turns again. The system prompt is sent on every call too.

A question it cannot answer

The company in my series is fictional. Its returns policy says 40 days from the delivery date. Asked for it, the model said it had no information about the company's policy and sent the customer to their receipt. That is the honest answer: nothing in the request put that file in front of it. It said so this time. A model that guessed a number instead would sound exactly as sure, and nothing in the response would tell you the number was guessed. The reply was 36 tokens in and 323 out, $0.0033 of advice. The fix is to put the right passage of that file in the request with the question.

Prompts are code

The next episode makes the output usable by code, two ways.

Structured output is JSON in a shape you wrote. The shape is written twice: as a TypeScript type for the compiler, and as JSON Schema, a standard way to describe a JSON document's fields, their types and which are required, for the API, which constrains the answer to it. The code still parses the text with JSON.parse into unknown and checks it with a type guard, a function that returns true only when a value has the right shape, because the model's output is input to your program. And if the answer reaches max_tokens, the JSON is cut off and JSON.parse throws. The run turned a customer's email into an order id, a name, two items with quantities and the intent, return, for $0.0019.

Tool use is the model asking your code to run a function. A tool is three things: a name, a description, and an input schema.

const tools: Tool[] = [ { name: "lookup_order", description: "Look up one order by its id, such as LS-2026-004871. Returns the order as JSON.", input_schema: { type: "object", properties: { order_id: { type: "string", description: "The order id" } }, required: ["order_id"], }, }, ]; 

The model never runs the function. It answers with a tool_use block, a request with an id and the input; your code runs the function, appends the model's whole turn and a tool_result to the conversation, and calls again, in a while loop you wrote, until the model answers in text. The run took two round trips and cost $0.0046, and every fact in its answer came from data/orders.json.

Look at what that added: a system prompt, a schema, and a tool description. They are strings and objects in TypeScript files, and they decide what the program does as much as any function does. Reword the description and you change when the model calls the tool. So they are treated as code: in the repository, reviewed in a pull request, and tested, from episode 7 on. The description is also in the request body on every call, so you pay for it as input every time.

What to try next: at tag s1e2, run src/tokens.ts on a sentence from your own documents and compare its word count to its token count. Then at s1e3, add a second tool beside lookup_order with a one-sentence description, ask a question that could use either, and see which the model picks.

First published at joelstevick.com/blog/calling-a-model-is-a-function-call.

Watch the series: https://www.youtube.com/@JoelStevick. Everything else, the demo and the book included: https://joelstevick.com