Letting Claude call your code
Tool use is how Claude reaches beyond text: you describe tools, Claude decides when to call one, you run it, and you feed the result back.
Key concept · Schemas, message blocks, and the loop
Each tool has a schema — name, description, and JSON input schema. When Claude wants one, the response contains a tool_use block (with an id and inputs) and the stop_reason is tool_use; you run the function and append a tool_result block (referencing that id) as the next user message, then call again — the same loop from Chapter 18, now with real tools. Claude can request multiple tools, chain them across turns, and fine-grained tool calling streams tool inputs as they're generated. Anthropic also ships built-in tools — like the text-editor tool and web search — that need no implementation from you.
Grounding answers in your data
Retrieval-Augmented Generation feeds Claude the relevant slice of your own documents at query time, so answers are grounded in your data rather than the model's training alone.
Key concept · The RAG flow, and making retrieval better
The flow: chunk your documents (chunking strategy matters), create embeddings for each chunk, store them, then at query time embed the question, retrieve the most similar chunks, and pass them to Claude as context. Beyond basic semantic search: BM25 lexical search catches exact keywords embeddings miss; a multi-index (hybrid) pipeline combines both; reranking reorders candidates by true relevance; and contextual retrieval prepends chunk-situating context before embedding so chunks aren't stranded from their source. Agentic search takes it further — Claude decides what to look up next, iteratively.
The features that make Claude more capable
A set of platform features extend what a single request can do.
Key concept · Thinking, multimodal, citations, caching, code execution
Extended thinking lets Claude reason before answering, for hard problems. Image support and PDF support make requests multimodal — Claude reads visual and document content directly. Citations make Claude point to the exact source passages behind a claim, so answers are verifiable. Prompt caching caches a large, stable prefix (a long system prompt, a big document) so repeated calls skip re-processing it — big latency and cost wins; the rules of prompt caching govern what's cacheable and how long it lives. And code execution (with the Files API) runs code in a sandbox for reliable math, data work, and file generation — the same capability that powers document creation.
Chapter 21 quiz
Fifteen questions on tool use, RAG, and features. Answer first, then expand Show answer.
Part A · Multiple choice
A tool definition includes:
- A Name, description, and a JSON input schema
- B A password
- C The model weights
- D A temperature
Show answer
A.
When Claude wants a tool, the response contains a tool_use block and you reply with:
- A A tool_result block referencing the tool_use id
- B A new API key
- C A system prompt
- D Nothing
Show answer
A.
Which are built-in tools that need no implementation?
- A The text-editor tool and web search
- B Your database
- C A custom CRM call
- D None exist
Show answer
A.
What does RAG do?
- A Feeds Claude the relevant slice of your documents at query time
- B Fine-tunes the model
- C Increases temperature
- D Caches the system prompt
Show answer
A. Grounds answers in your data.
The basic RAG flow is:
- A Chunk → embed → store → retrieve similar chunks → pass to Claude
- B Train → deploy → serve
- C Prompt → cache → stream
- D Compile → run → commit
Show answer
A.
What does BM25 add to a RAG pipeline?
- A Lexical (keyword) search that catches exact terms embeddings miss
- B A bigger model
- C Prompt caching
- D Streaming
Show answer
A. Combined with semantic search in a hybrid pipeline.
What does prompt caching save you?
- A Re-processing a large stable prefix on repeated calls — cutting latency and cost
- B The need for an API key
- C Writing tools
- D Nothing
Show answer
A.
What do Citations provide?
- A Pointers to the exact source passages behind a claim
- B Faster responses
- C A JSON schema
- D A new model
Show answer
A. Making answers verifiable.
Part B · True or false
Claude can request multiple tools and chain them across turns.
Show answer
True.
Contextual retrieval prepends situating context to a chunk before embedding it.
Show answer
True. So chunks aren't stranded from their source.
Image and PDF support make a request multimodal.
Show answer
True. Claude reads visual and document content directly.
Code execution is useful for reliable math, data work, and file generation.
Show answer
True. With the Files API; it powers document creation.
Reranking reorders retrieved candidates by true relevance.
Show answer
True.
Part C · Short answer
Name the steps of the basic RAG flow.
Show answer
Chunk documents, embed the chunks, store them, embed the query, retrieve the most similar chunks, pass them to Claude.
Name four features of Claude covered here.
Show answer
Any four: extended thinking, image support, PDF support, citations, prompt caching, code execution (Files API).