Claude With The Anthropic Api
← All lessons
Lesson 35Claude With The Anthropic Api

Handling message blocks

Summary audio

No audio recap for this lesson.

Study notes

Making Requests with Tools

  • Include a tools keyword argument when calling client. messages. create()
  • The tools argument is a list containing JSON schema specifications for available tools
  • This tells Claude which tools it can use to respond to user queries

Multi-Block Messages

  • When Claude uses a tool, the response message contains multiple content blocks, not just text
  • A tool-use response typically includes:
  • Text block: explanatory text for the user about what Claude is doing
  • Tool use block: specifies which tool Claude wants to call and what arguments to pass

Tool Use Block Structure

  • Contains the name of the tool function Claude wants to call
  • Includes the input arguments/parameters needed for that function
  • Signals to developers that a tool execution is required

Managing Conversation History with Tools

  • Claude does not store conversation history automatically
  • Developers must manually maintain the full conversation history
  • When appending assistant messages to the message list, include the entire content field (all blocks)
  • This ensures Claude receives complete context when making follow-up requests

Updating Helper Functions

  • Existing helper functions like add_user_message() and add_assistant_message() need updates
  • They must support messages with multiple content blocks, not just single text blocks
  • This allows proper handling of tool-use responses in the conversation flow

Takeaways

  • Claude can use tools when you include a tools argument with JSON schema specifications in client. messages. create()
  • Tool-use responses contain multiple content blocks: text explaining the action plus a tool-use block specifying which tool to call and its arguments
  • Developers must manually maintain full conversation history, including all content blocks from assistant messages, so Claude has complete context for follow-up requests
  • Helper functions need updates to handle multi-block messages instead of just single text blocks
Flashcards 8 cards
Question
click to reveal · ←/→
Answer
click to flip back
Export to Anki (.tsv) ↓
Knowledge check 1 questions