Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Build LLM-powered apps with the Anthropic Claude API or SDK across Python, TypeScript, Java, Go, Ruby, C#, and PHP.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
go/claude-api/streaming.md
1# Streaming — Go23## Streaming45```go6stream := client.Messages.NewStreaming(context.Background(), anthropic.MessageNewParams{7Model: anthropic.ModelClaudeOpus4_8,8MaxTokens: 64000,9Messages: []anthropic.MessageParam{10anthropic.NewUserMessage(anthropic.NewTextBlock("Write a haiku")),11},12})1314for stream.Next() {15event := stream.Current()16switch eventVariant := event.AsAny().(type) {17case anthropic.ContentBlockDeltaEvent:18switch deltaVariant := eventVariant.Delta.AsAny().(type) {19case anthropic.TextDelta:20fmt.Print(deltaVariant.Text)21}22}23}24if err := stream.Err(); err != nil {25log.Fatal(err)26}27```2829**Accumulating the final message** (there is no `GetFinalMessage()` on the stream):3031```go32stream := client.Messages.NewStreaming(ctx, params)33message := anthropic.Message{}34for stream.Next() {35message.Accumulate(stream.Current())36}37if err := stream.Err(); err != nil { log.Fatal(err) }38// message.Content now has the complete response39```404142---4344