Skip to content

Streaming

Arian Amiramjadi edited this page Dec 24, 2025 · 1 revision

Streaming

Stream responses to see text appear as it's generated.

Basic Streaming

// Print directly to stdout
ai.GPT5().
    System("You are a storyteller").
    Stream("Tell me a story")

Custom Callback

// Handle each chunk yourself
ai.Claude().
    System("Be creative").
    User("Write a poem").
    StreamResponse(func(chunk string) {
        // chunk is a small piece of the response
        fmt.Print(chunk)
        
        // Or do something else:
        // - Send to WebSocket
        // - Update UI
        // - Log to file
    })

Full Example

package main

import (
    "fmt"
    ai "gopkg.in/dragon-born/go-llm.v1"
)

func main() {
    fmt.Println("Starting story...")
    
    response, err := ai.GPT5().
        System("You are a creative storyteller").
        Stream("Tell me a short story about a robot")
    
    if err != nil {
        fmt.Printf("Error: %v\n", err)
        return
    }
    
    // response contains the full text after streaming completes
    fmt.Printf("\n\nFull response length: %d chars\n", len(response))
}

Streaming vs Non-Streaming

Aspect Send() Stream()
Response Waits for complete Prints as it arrives
UX Slower perceived Faster perceived
Return Full text Full text (after done)
Tokens Exact count Estimated

Combining with Other Features

// Streaming with system file
ai.Claude().
    SystemFile("prompts/creative.md").
    Stream("Write something beautiful")

// Streaming with temperature/thinking
ai.GPT5().
    Temperature(0.2).
    ThinkLow().
    Stream("Write a short sci-fi opening")

// Streaming with variables
ai.Prompt("storyteller").
    Var("genre", "sci-fi").
    Stream("Begin the tale")

// Note: Retry/Fallback don't work with streaming
// (would need to restart the stream)

Notes

  • Streaming uses Server-Sent Events (SSE) under the hood
  • Token counts are estimated (based on response length)
  • Pretty mode adds model name header before streaming
  • Debug mode shows request before streaming starts

Clone this wiki locally