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

Sending tool results

Summary audio

No audio recap for this lesson.

Study notes

Step 4: Running the Tool Function

  • Access the tool use block from Claude's response via response. content[1]
  • Extract the input arguments (dictionary) using . input property
  • Convert the dictionary to keyword arguments using the ** operator when calling the function
  • Example: get_current_datetime(**response. content[1]. input)

Step 5: Sending Follow-up Request with Tool Results

Tool Result Block Structure

  • Type: tool_result (placed inside a user message)
  • Purpose: Feeds the tool function's output back to Claude
  • Key properties:
  • tool_use_id: Matches the ID from the original tool use block to link request with result
  • content: The output from the tool function (converted to string/JSON)
  • is_error: Boolean flag (default false) indicating if the tool execution failed

Why Tool Use IDs Matter

  • Claude may request multiple tool calls in one response
  • Each tool use block gets a unique ID
  • Tool result blocks must reference the correct ID to match results with requests
  • Prevents ambiguity when multiple tools are called

Complete Conversation Flow

  • Send user message + tool schema to Claude
  • Claude responds with assistant message containing tool use block(s)
  • Execute the tool function(s) locally
  • Send follow-up request with:
  • Full conversation history (original user message + assistant response)
  • New user message containing tool result block(s)
  • Original tool schema(s) again
  • Claude processes the tool result and sends final response

Takeaways

  • Extract tool inputs from Claude's response using . input property and call the function with ** operator to unpack arguments
  • Tool result blocks must include tool_use_id to match results with the correct tool call when multiple tools are invoked
  • Follow-up requests require the full conversation history, tool result block(s) in a user message, and the original tool schema(s) for Claude to process results correctly
Flashcards 9 cards
Question
click to reveal · ←/→
Answer
click to flip back
Export to Anki (.tsv) ↓
Knowledge check 1 questions