Skip to content

Workflow: Streaming Conversation with Prompt Caching ​

Updated: 2026-05-26

End-to-end streaming conversation flow with prompt caching optimization.

sequenceDiagram
    participant App as Your Application
    participant API as Messages API
    participant Cache as Prompt Cache

    App->>App: Build messages array
    App->>App: Mark static blocks with cache_control
    App->>API: POST /messages (stream: true)
    API->>Cache: Check for cache hit
    alt Cache Hit
        Cache-->>API: Return cached prefixes
    else Cache Miss
        API->>Cache: Write new cache entry
    end
    API-->>App: event: message_start
    loop For each content chunk
        API-->>App: event: content_block_delta
    end
    API-->>App: event: message_delta
    API-->>App: event: message_stop
    App->>App: Accumulate text from deltas
    App->>App: Display final response

Phases ​

Phase 1: Build Request ​

Assemble the messages array with conversation history. Mark the last static block with cache_control: { type: "ephemeral" } before any dynamic content.

Phase 2: Cache Check ​

The API checks if the marked prefix matches an existing cache entry. A cache hit reads cached tokens at reduced cost. A miss writes new cache entries.

Phase 3: Stream Response ​

The server sends events in order:

  • message_start: Message begins, includes role and initial content blocks
  • content_block_delta: Partial text chunks for real-time display
  • message_delta: Final content block closure and stop reason
  • message_stop: Complete message with full usage data

Phase 4: Accumulate and Display ​

Your application buffers the delta text and renders it incrementally for a streaming UI experience.

See Also ​