Tool Use & Function Calling

How do you handle streaming responses when tools are called?

QUICK ANSWER

In stream responses, listen for events of type 'content_block_start' with content type 'tool_use'. Collect arguments from 'content_block_delta' events, and execute the tool once the block completes.

Stream Parsing Logic Example

for await (const event of stream) {
  if (event.type === 'content_block_start' && event.content_block.type === 'tool_use') {
    currentToolCall = {
      id: event.content_block.id,
      name: event.content_block.name,
      arguments: ""
    };
  }
  if (event.type === 'content_block_delta' && event.delta.type === 'input_json_delta') {
    currentToolCall.arguments += event.delta.partial_json;
  }
}
Verified against: Anthropic Streaming API Specification