Speeding Up the Agent › Streaming Responses

Michael BrenndoerferAugust 24, 20251 min read
AI Agent HandbookAll chapters
Part 15 of 15Performance and Cost Optimization - Doing More with Less
Chapter 42Speeding Up the Agent › Streaming Responses
Reading section 7 of 15 · Streaming Responses · 1 minSection 7 of 15 · 1 min

Book contents

Section 7 of 15 · 1 min read

Speeding Up the Agent › Streaming ResponsesOne section · Sections in this chapter7 / 15 ↓

Sometimes you can't make the agent faster, but you can make it feel faster. Streaming responses show results as they're generated, rather than waiting for the complete answer.

Example: Streaming for Perceived Speed (Claude Sonnet 4.5)

In[7]:
Code
import os
from anthropic import Anthropic

client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))

def stream_response(query):
    """Stream the response as it's generated."""
    # Using Claude Sonnet 4.5 with streaming enabled
    with client.messages.stream(
        model="claude-sonnet-4-5",
        max_tokens=1024,
        messages=[{"role": "user", "content": query}]
    ) as stream:
        for text in stream.text_stream:
            print(text, end="", flush=True)
    print()  # New line at the end

## Try it out
print("Streaming response:")
stream_response("Explain what an API is in simple terms.")
Out[7]:
Console
Streaming response:
#
 What is an API?

An **
API (
Application Programming Interface)** is like
 a messenger
 that lets
 different software programs
 talk to each other.


## Simple
 Analogy


Think
 of a
 restaurant
:

- **
You
** (the
 customer
) are
 one
 program

- **The
 kitchen
** (where
 foo
d is made) is another program
-
 **The waiter** is
 the API


You
 don
't go
 into
 the kitchen to
 cook
.
 Instea
d, you tell
 the waiter what you want,
 the
 wa
iter takes
 your order to
 the kitchen, and then
 brings
 back
 your
 food.
 The wa
iter is the go
-between that
 makes
 everything
 work smooth
ly.


## Real
-World Example

When
 you use a
 weather
 app on
 your phone:
- The
 app doesn
't store
 all
 the weather data itself

- It
 uses an API to ask
 a
 weather
 service
 "
What's the weather in
 New York?"
- The API sends
 back
 the
 information

- Your app
 displays it nic
ely for
 you


## Why
 APIs Matter


They
 let
 developers
:
- Use
 features
 from
 other services
 without rebuil
ding them

- Connect
 different apps
 and services together
- Save
 time and effort


**
Bottom
 line
:** APIs are the behin
d-the-scenes conn
ectors that make modern apps
 an
d websites
 work together.

The total time to generate the response doesn't change, but the user sees words appearing immediately. This makes the agent feel much more responsive. Instead of staring at a blank screen for three seconds, they see the answer forming in real time.