Claude and the Anthropic SDK (TypeScript) · Chapter 11 of 21
PDFs and citations
By the end of this chapter you can send Claude a PDF as it is, know what its pages cost in tokens, send a plain text document with a title, and turn on citations so every answer about the policy comes with the exact words it was based on, which you can show a family. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Tools and structure
- 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
Hospitals send discharge summaries as PDFs: two pages, the patient's date of birth, the diagnosis, and the bill summary on page 2. Kamla has been copying the numbers out by hand before she asks Sahay anything. A PDF is not text your program can paste into a message: it is a file of pages, fonts and drawing commands. Claude can read it directly, if the request carries it the right way.
And families do not take Sahay's word for it. When Sahay says "the ambulance is paid only up to ₹2,000", the family asks "where does it say that?". Meher wants every policy answer to come with the policy's own words, and their place in the wording, so Kamla can paste the line into WhatsApp. Asking Claude to "quote the clause" in the prompt gives you whatever it writes; citations give you the quoted text and where it is, as data, checked against the document you sent.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
read-a-pdf.ts: a discharge summary
Send 'claude-opus-5-5' (max_tokens 4096, system: SYSTEM, given) one user message with two blocks: a document block whose source is pdf('discharge-CLM-2414.pdf'), then a text block, exactly: How much will the policy pay for this claim?
Print the text of the reply's text blocks, then input tokens: N (reply.usage.input_tokens).
- Task 2
pdf-tokens.ts: what does a PDF cost?
Send 'claude-opus-5-5' (max_tokens 1024) the question What is this? twice: first with a document block of pdf('discharge-CLM-2416.pdf') before it, then alone. The PDF's tokens are the difference of the two input_tokens.
Print, in this order: pages: P (pageCount(FILE), given), with the PDF: N tokens, without it: M tokens, then the PDF: D tokens, X for the pages and Y for the text, where X is P x 1500 and Y is D minus X.
- Task 3
cite-the-policy.ts: where does it say that?
Send 'claude-opus-5-5' (max_tokens 2048, no system) one user message: a document block with the policy as plain text (source: { type: 'text', media_type: 'text/plain', data: policyWording() }), title 'Parchi Suraksha policy wording' and citations: { enabled: true }, then the text block Is the ambulance paid?
Print the answer (the text of every text block, joined), then one line per citation, numbered from 1: [N] "CITED_TEXT" (characters START-END), from cited_text, start_char_index and end_char_index.
- Task 4 · interview question
Interview: json-with-citations.ts
"Farhan wants Sahay's policy answers as JSON with their quotes, and the request fails. Why, and how do you give him the JSON?" Send 'claude-opus-5-5' (max_tokens 2048) the cited policy document of task 3 and the question Is knee replacement covered in full?, with output_config: { format: { type: 'json_schema', schema } } (any object schema). Catch the Anthropic.BadRequestError and print refused: STATUS TYPE (err.status, err.type).
Send it again without output_config, citations still on, and print on one line JSON.stringify({ answer, quotes }): answer is every text block's text joined, quotes is one { text, start, end } per citation (cited_text, start_char_index, end_char_index).
What you will learn
- Document blocks: a content block that carries a PDF (base64) or plain text, beside the text of the question.
- What a PDF costs: tokens for each page as an image, plus its text.
- title: a name for a document, which Claude and the citations see.
- Citations: citations: { enabled: true }, cited text blocks, and char_location with its character positions (page_location for PDFs).
- Citations and structured output cannot go in one request, and what to do instead.
More in Tools and structure
Build "PDFs and citations" 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.