Claude With Amazon Bedrock
← All lessons
Lesson 29Claude With Amazon Bedrock

Handling tool use responses

Summary audio

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

Study notes

Tool Use with LLMs (Claude)

🛠️ Configuring Tools in the Request

  • Tool Inclusion: When making a request to Claude, include the JSON schema specification that describes how to call your tool.
  • tools Argument: Add a tools keyword argument to the chat function, which accepts a list of your tool schemas.
  • tool_config (Advanced Control): This nested dictionary controls how the model decides to use the provided tools.
  • auto (Default): The model delegates the choice of whether or not to use a tool to itself.
  • any (String): Forces Claude to use a tool, and it decides which one.
  • Specific Tool Name (Nested Dictionary): Forces Claude to call a specific tool by providing its name.

🤖 Understanding the Model Response (Tool Use)

When the model decides to use a tool, the response structure changes significantly:

  • stop_reason: This key indicates why the model stopped generating output.
  • If the model decides to use a tool, the stop_reason will be tool_use.
  • Assistant Message Structure: The assistant message's content list will contain multiple parts, not just text.
  • Text Part: Contains any helpful introductory text from the model (e. g. , "I can check the current date time for you. ").
  • Tool Use Part: A specific dictionary indicating the model's intent to run a tool.
  • name: The exact name of the tool from your JSON schema that the model wants to run.
  • input: A dictionary containing the arguments that the model wants you to pass into your tool function.

🔄 Necessary Code Refactoring

  • Handling Multi-Part Messages: Since the assistant message now contains multiple parts (Text + Tool Use), the existing message handling functions must be updated.
  • chat Function Update: The function must now return both the extracted text and the full list of message parts.
  • Message Management Functions (add_user_message, add_assistant_message): These functions were refactored to be flexible:
  • They must accept either a simple string (for basic text) or a list of parts (for complex tool-use responses).
  • The logic was updated to correctly wrap a single string into a single text part when necessary.

Takeaways

  • Tools are configured by including a JSON schema specification within the tools argument of the request.
  • When a tool is used, the model response features a stop_reason of tool_use and a multi-part assistant message.
  • The tool use part of the message contains the exact tool name and a dictionary of required input arguments.
  • Message handling functions must be updated to accept and process messages that are either a simple string or a complex list of parts.
Flashcards 10 cards
Question
click to reveal · ←/→
Answer
click to flip back
Export to Anki (.tsv) ↓
Knowledge check 1 questions