Claude and the Anthropic SDK (TypeScript) · Chapter 20 of 21

Capstone: Sahay, end to end

By the end of this chapter Sahay does a whole day of Parchi's work: it reads a family's upload (a bill photo or a discharge summary PDF) into a typed record, works out what the policy pays with tools, thinking and a cached policy, shows a family the policy's own words with citations, sorts the WhatsApp inbox in a batch, and ends the day with a cost report that Farhan can check against the bill. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Ship it
  • 4 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of Claude and the Anthropic SDK (TypeScript) are free. This one comes with the all labs plan.

Why you need this

Meher's goal has not changed since chapter 1: four hundred bills a week, read once by Sahay before Kamla's team reads them. Over twenty chapters you built every piece of that separately. A photo became a record in chapter 10, a PDF in chapter 11, the tool loop learned to keep its thinking and read the cache in chapter 19, and the inbox went into a batch in chapter 17. Each piece worked on the example it was written for.

A real day does not come in pieces. A family sends a photo and a question in the same message. Another sends a discharge summary. A third asks whether the policy pays for ICU rent and wants to see where it says so. Sixty WhatsApp messages wait to be sorted, and nobody is waiting for those. Meher wants one Sahay that handles all of it, and Farhan wants to know, before Parchi signs up for four hundred bills a week, what a bill costs. The pieces you have do not answer that: you have never run them together, on the model you would ship, with the bill added up.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    intake.ts: a photo or a PDF into a record

    Export Upload (a zod object: claim_id nullable string, patient string, hospital nullable string, total number), uploadBlock(file) (a .pdf as a document block with pdf(file), anything else as an image block with billPhoto(file)) and readUpload(file): messages.parse with 'claude-sonnet-5-5', max_tokens 4096, [uploadBlock(file), a text block] as the one user message and output_config: { format: zodOutputFormat(Upload) }, returning parsed_output.

    Under if (import.meta.main), print FILE: CLAIM, PATIENT, HOSPITAL, TOTAL for bill-CLM-2408.png and discharge-CLM-2401.pdf (no claim id, no hospital for nulls). The check reads uploads the task never named, a pharmacy slip among them.

  2. Task 2

    sahay.ts: Sahay, end to end

    Export ask(question, { upload, model, maxSteps }) (defaults 'claude-sonnet-5-5' and 8) returning { answer, stop, requests, dollars }. The first user message is the question, or, with an upload, [uploadBlock(upload), the question as a text block]. Then chapter 19's loop with the given turn, toolResults and dollars: push each reply exactly as it came, return the text when it stops for anything but tool_use, stop at maxSteps with answer '' and stop 'max_steps'.

    Under if (import.meta.main), ask How much will the policy pay for this? with bill-CLM-2408.png and print the answer, then N requests, $D. The check asks claims, photos and PDFs the task never named.

  3. Task 3

    cite.ts: show the family where it says so

    Export explain(question): one request to 'claude-sonnet-5-5' (max_tokens 2048) whose user message is [the policy as a text document with title 'Parchi Suraksha policy wording', citations: { enabled: true } and cache_control: { type: 'ephemeral' }, the question]. Return { text, quotes }: the text blocks' text joined, and the cited_text of every citation in order.

    Under if (import.meta.main), print the text and then [N] "QUOTE" for each quote, for Is the ambulance paid? and Are gloves and masks paid?. The check asks questions the task never named, and every one after the first must read the cache.

  4. Task 4

    report.ts: the day's run, and what it cost

    With ask() and dollars() from sahay.ts: ask How much will ID pay? for every claim (Sonnet 5.5), ask How much will the policy pay for this? with each of UPLOADS, and sort the inbox in one batch (custom_id m.id, 'claude-haiku-5-5', max_tokens 1024, system: SORT), each succeeded result priced with dollars(usage, 'claude-haiku-5-5') x BATCH_DISCOUNT.

    Print claims: 16 answered, N requests, $D; uploads: 2 answered, N requests, $D; inbox: N sorted in one batch, $D; total: $D (6 decimals). All sixteen answers must be right.

What you will learn

  • Putting the pieces together: one request shape for every turn, an upload in the first message, the same loop for a question with or without a photo.
  • Choosing the model with numbers: the cheapest model that gets all sixteen claims right, measured, not guessed.
  • What an upload costs: photo and PDF tokens, paid in full once and read from the cache after that.
  • Citations on a cached document: the family sees the clause, and the policy is paid for once.
  • A day's bill: real time where someone waits, a batch where nobody does, and the report that adds it up.

More in Ship it

Build "Capstone: Sahay, end to end" yourself

Parchi, a Jaipur startup, helps families claim health insurance. You build Sahay, a claims assistant that reads bills and policies with Claude, and measure what caching, models and effort cost. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Claude and the Anthropic SDK (TypeScript) are free. This one comes with the all labs plan.