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

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 for Tool-Using Conversations

  • The Problem with Simple Queries: When a query does not require tool use (e. g. , "what is one plus one"), the system must correctly identify the end of the conversation to avoid erroneously adding empty tool-use messages.
  • The Role of stop_reason: To manage long-running conversations involving tools, the system must inspect the stop_reason returned in the API response. This reason dictates whether the conversation needs further processing or tool execution.
  • Refactoring the chat Function:
  • The chat function must be updated to return a structured dictionary, not just text.
  • The returned dictionary must include:
  • parts (the list of message parts).
  • stop_reason (the reason the model stopped generating).
  • text (a consolidated string created by joining all text parts from the message).
  • Implementing the Conversation Loop (run_conversation):
  • A multi-turn conversation requires a loop (e. g. , while True) to handle the back-and-forth between the model and the tools.
  • Loop Logic:
  • Call the chat function with the current message history.
  • Add the assistant's response parts to the message history.
  • Check stop_reason:
  • If stop_reason is NOT equal to "tool use" → Break the loop (the conversation is complete).
  • If stop_reason IS "tool use" → Proceed to tool execution.
  • Execute the necessary tools using run_tools with the requested tool parts.
  • Add the resulting tool output as a user message back into the history.
  • Conversation Flow: This loop ensures that the conversation continues until the assistant provides a final response that does not request any further tool calls.

Takeaways

  • Multi-turn conversations require a loop to manage the back-and-forth between the model and external tools.
  • The stop_reason returned by the API is critical for determining if the conversation requires further tool execution or if it is complete.
  • The chat function must be updated to return a structured dictionary containing parts, stop_reason, and a consolidated text.
  • The conversation loop proceeds to tool execution only if the stop_reason is "tool use"; otherwise, the loop breaks.
Flashcards 8 cards
Question
click to reveal · ←/→
Answer
click to flip back
Export to Anki (.tsv) ↓
Knowledge check 5 questions