Claude On Google Cloud
← All lessons
Lesson 33Claude On Google Cloud

Multi-turn conversations with tools

Summary audio

Spoken summary — press play to read along: the line being spoken stays near the top.

Study notes

Key Concepts: Implementing Multi-Tool Calling with Claude

**I. Core Concept: Sequential Tool Use**

  • Goal: The primary objective is to enable Claude to use multiple tools in sequence to answer a single complex user query.
  • Process Flow:
  • User submits a query.
  • Claude responds with a request to use Tool 1.
  • The application executes Tool 1 and returns the result to Claude.
  • Claude analyzes the result and determines it needs Tool 2.
  • The application executes Tool 2 and returns the result to Claude.
  • Claude, now having all necessary information, provides the final answer.
  • Crucial Assumption: When building the application, you must assume that any user query may require multiple, sequential tool calls.

**II. Implementation Strategy: The Conversation Loop**

  • Function: A function (e. g. , runConversation) must be implemented to manage the interaction.
  • Mechanism: Use a while loop to continuously interact with Claude.
  • Loop Logic:
  • Check Response: Examine Claude's response.
  • If Tool Requested: Execute the requested tool, capture the result, add the result to the message history, and call Claude again (continue the loop).
  • If No Tool Requested: The response is the final answer; break the loop and deliver the result to the user.

**III. Necessary Code Refactoring** To support multiple tool calls and complex responses, the following components must be updated:

  • **A. Message Helper Functions (addUserMessage, addAssistantMessage):**
  • Change: Must be upgraded to handle complex message objects, not just plain text strings.
  • Function: They must accept the entire message response from Claude, which may contain multiple blocks (e. g. , text blocks and tool use blocks).
  • **B. chat Function:**
  • Tool Integration: Must accept a list of tool schemas and pass them to the client's message creation function.
  • Return Value Change: Must return the entire message object received from Claude, rather than just extracting the text block. This is necessary because the response may contain multiple blocks.
  • **C. New Utility Function (textFromMessage):**
  • Purpose: A helper function designed to iterate through all blocks within a message.
  • Function: It extracts and joins the text content from every block where the type is identified as "text. "

Takeaways

  • Multi-tool calling requires a sequential process where Claude iteratively uses tools until the complex user query is fully resolved.
  • Implement a conversation loop (while loop) that continues to call Claude as long as the response contains a request to use a tool.
  • Message helper functions must be upgraded to handle complex message objects that contain both text blocks and tool use blocks.
  • The chat function must return the entire message object received from Claude, not just the extracted text.
  • A new utility function (textFromMessage) is required to iterate through and extract text content from all blocks within a message.
Flashcards 10 cards
Question
click to reveal · ←/→
Answer
click to flip back
Export to Anki (.tsv) ↓
Knowledge check 1 questions