Loading...
    • Erstellen
    • Admin
    • Modelle & Preise
    • Client SDKs
    • API-Referenz
    Search...
    ⌘K
    Erste Schritte
    Einführung in ClaudeSchnellstart
    Entwickeln mit Claude
    FunktionsübersichtVerwendung der Messages APIStoppgründe behandeln
    Modellfähigkeiten
    Erweitertes DenkenAdaptives DenkenAufwandSchnellmodus (Beta: Forschungsvorschau)Strukturierte AusgabenZitateStreaming-NachrichtenStapelverarbeitungSuchergebnisseStreaming-AblehnungenMehrsprachige UnterstützungEmbeddings
    Tools
    ÜbersichtWie Tool-Nutzung funktioniertWeb-SuchtoolWeb-AbruftoolCode-AusführungstoolSpeichertoolBash-ToolComputer-Use-ToolTexteditor-Tool
    Tool-Infrastruktur
    Tool-SucheProgrammatisches Tool-AufrufenFeingranulares Tool-Streaming
    Kontextverwaltung
    KontextfensterKomprimierungKontextbearbeitungPrompt-CachingToken-Zählung
    Mit Dateien arbeiten
    Files APIPDF-UnterstützungBilder und Vision
    Skills
    ÜbersichtSchnellstartBest PracticesSkills für UnternehmenSkills in der API
    MCP
    Remote-MCP-ServerMCP-Connector
    Prompt-Engineering
    ÜbersichtBest Practices für PromptsConsole-Prompting-Tools
    Testen und Evaluieren
    Erfolg definieren und Evaluierungen erstellenDas Evaluierungstool in der Console verwendenLatenz reduzieren
    Leitplanken stärken
    Halluzinationen reduzierenAusgabekonsistenz erhöhenJailbreaks abmildernPrompt-Lecks reduzieren
    Ressourcen
    Glossar
    Versionshinweise
    Claude Platform
    Console
    Log in
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Partners

    • Amazon Bedrock
    • Google Cloud's Vertex AI

    Learn

    • Blog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Learn

    • Blog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Help and security

    • Availability
    • Status
    • Support
    • Discord

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    Modellfähigkeiten

    Streaming von Nachrichten

    Erfahren Sie, wie Sie Nachrichten mit Claude streamen können, um Antworten inkrementell zu erhalten.

    Beim Erstellen einer Message können Sie "stream": true setzen, um die Antwort inkrementell mit Server-Sent Events (SSE) zu streamen.

    Streaming mit SDKs

    Die Python und TypeScript SDKs bieten mehrere Möglichkeiten zum Streamen. Das PHP SDK bietet Streaming über createStream(). Das Python SDK ermöglicht sowohl synchrone als auch asynchrone Streams. Weitere Informationen finden Sie in der Dokumentation der einzelnen SDKs.

    Was this page helpful?

    • Streaming mit SDKs
    • Abrufen der endgültigen Nachricht ohne Verarbeitung von Ereignissen
    • Ereignistypen
    • Ping-Ereignisse
    • Fehlerereignisse
    • Andere Ereignisse
    • Content-Block-Delta-Typen
    • Text-Delta
    • Input-JSON-Delta
    • Thinking-Delta
    • Vollständige HTTP-Stream-Antwort
    • Grundlegende Streaming-Anfrage
    • Streaming-Anfrage mit Tool-Nutzung
    • Streaming-Anfrage mit erweitertem Denken
    • Streaming-Anfrage mit Web-Such-Tool-Nutzung
    • Fehlerwiederherstellung
    • Claude 4.5 und früher
    • Claude 4.6
    • Best Practices zur Fehlerwiederherstellung
    ant messages create --stream --format jsonl \
      --model claude-opus-4-6 \
      --max-tokens 1024 \
      --message '{role: user, content: "Hello"}' \
      | while IFS= read -r event; do
          [[ $event == *'"text_delta"'* ]] || continue
          text=${event#*'"text":"'}
          printf '%b' "${text%\"*}"
        done

    Abrufen der endgültigen Nachricht ohne Verarbeitung von Ereignissen

    Wenn Sie Text nicht bei der Ankunft verarbeiten müssen, bieten die SDKs eine Möglichkeit, Streaming im Hintergrund zu verwenden und gleichzeitig das vollständige Message-Objekt zurückzugeben, das identisch mit dem ist, was .create() zurückgibt. Dies ist besonders nützlich für Anfragen mit großen max_tokens-Werten, bei denen die SDKs Streaming benötigen, um HTTP-Timeouts zu vermeiden.

    # Das ant CLI-Flag --stream gibt ein Ereignis pro Zeile aus und
    # sammelt nicht zu einer endgültigen Message. Für lange Generierungen
    # streamen Sie die Raw-Ereignisse:
    ant messages create --stream --format jsonl <<'YAML'
    model: claude-opus-4-6
    max_tokens: 128000
    messages:
      - role: user
        content: Write a detailed analysis...
    YAML

    Der .stream()-Aufruf hält die HTTP-Verbindung mit Server-Sent Events aktiv, dann akkumuliert .get_final_message() (Python) oder .finalMessage() (TypeScript) alle Ereignisse und gibt das vollständige Message-Objekt zurück. In Go rufen Sie message.Accumulate(event) in der Stream-Schleife auf, um die gleiche vollständige Message zu erstellen. In Java verwenden Sie MessageAccumulator.create() und rufen accumulator.accumulate(event) für jedes Ereignis auf. In Ruby rufen Sie .accumulated_message auf dem Stream auf. Im PHP SDK iterieren Sie manuell über Stream-Ereignisse, um die Antwort zu akkumulieren.

    Ereignistypen

    Jedes Server-Sent Event enthält einen benannten Ereignistyp und zugehörige JSON-Daten. Jedes Ereignis verwendet einen SSE-Ereignisnamen (z. B. event: message_stop) und enthält den entsprechenden Ereignis-type in seinen Daten.

    Jeder Stream verwendet den folgenden Ereignisfluss:

    1. message_start: enthält ein Message-Objekt mit leerem content.
    2. Eine Serie von Content-Blöcken, von denen jeder ein content_block_start, ein oder mehrere content_block_delta-Ereignisse und ein content_block_stop-Ereignis hat. Jeder Content-Block hat einen index, der seinem Index im finalen Message-content-Array entspricht.
    3. Ein oder mehrere message_delta-Ereignisse, die Änderungen auf oberster Ebene am finalen Message-Objekt anzeigen.
    4. Ein finales message_stop-Ereignis.

    Die Token-Zählungen, die im usage-Feld des message_delta-Ereignisses angezeigt werden, sind kumulativ.

    Ping-Ereignisse

    Event-Streams können auch eine beliebige Anzahl von ping-Ereignissen enthalten.

    Fehlerereignisse

    Die API kann gelegentlich Fehler im Event-Stream senden. Beispielsweise können Sie während Zeiten hoher Auslastung einen overloaded_error erhalten, der normalerweise einem HTTP 529 in einem nicht-Streaming-Kontext entsprechen würde:

    Example error
    event: error
    data: {"type": "error", "error": {"type": "overloaded_error", "message": "Overloaded"}}

    Andere Ereignisse

    Gemäß der Versionierungsrichtlinie können neue Ereignistypen hinzugefügt werden, und Ihr Code sollte unbekannte Ereignistypen elegant verarbeiten.

    Content-Block-Delta-Typen

    Jedes content_block_delta-Ereignis enthält ein delta eines Typs, der den content-Block bei einem bestimmten index aktualisiert.

    Text-Delta

    Ein text Content-Block-Delta sieht wie folgt aus:

    Text delta
    event: content_block_delta
    data: {"type": "content_block_delta","index": 0,"delta": {"type": "text_delta", "text": "ello frien"}}

    Input-JSON-Delta

    Die Deltas für tool_use Content-Blöcke entsprechen Aktualisierungen für das input-Feld des Blocks. Um maximale Granularität zu unterstützen, sind die Deltas partielle JSON-Strings, während das finale tool_use.input immer ein Objekt ist.

    Sie können die String-Deltas akkumulieren und das JSON analysieren, sobald Sie ein content_block_stop-Ereignis erhalten, indem Sie eine Bibliothek wie Pydantic zur partiellen JSON-Analyse verwenden, oder indem Sie die SDKs verwenden, die Helfer für den Zugriff auf analysierte inkrementelle Werte bereitstellen.

    Ein tool_use Content-Block-Delta sieht wie folgt aus:

    Input JSON delta
    event: content_block_delta
    data: {"type": "content_block_delta","index": 1,"delta": {"type": "input_json_delta","partial_json": "{\"location\": \"San Fra"}}

    Hinweis: Aktuelle Modelle unterstützen nur das Ausgeben einer vollständigen Schlüssel- und Werteigenschaft aus input auf einmal. Daher kann es bei der Verwendung von Tools zu Verzögerungen zwischen Streaming-Ereignissen kommen, während das Modell arbeitet. Sobald ein input-Schlüssel und -Wert akkumuliert sind, werden sie als mehrere content_block_delta-Ereignisse mit segmentiertem partiellem JSON ausgegeben, damit das Format automatisch feinere Granularität in zukünftigen Modellen unterstützen kann.

    Thinking-Delta

    Bei Verwendung von Extended Thinking mit aktiviertem Streaming erhalten Sie Thinking-Inhalte über thinking_delta-Ereignisse. Diese Deltas entsprechen dem thinking-Feld der thinking Content-Blöcke.

    Für Thinking-Inhalte wird ein spezielles signature_delta-Ereignis kurz vor dem content_block_stop-Ereignis gesendet. Diese Signatur wird verwendet, um die Integrität des Thinking-Blocks zu überprüfen.

    Wenn display: "omitted" in der Thinking-Konfiguration gesetzt ist, werden keine thinking_delta-Ereignisse gesendet. Der Thinking-Block öffnet sich, empfängt ein einzelnes signature_delta und schließt sich. Siehe Controlling thinking display.

    Ein typisches Thinking-Delta sieht wie folgt aus:

    Thinking delta
    event: content_block_delta
    data: {"type": "content_block_delta", "index": 0, "delta": {"type": "thinking_delta", "thinking": "I need to find the GCD of 1071 and 462 using the Euclidean algorithm.\n\n1071 = 2 × 462 + 147"}}

    Das Signature-Delta sieht wie folgt aus:

    Signature delta
    event: content_block_delta
    data: {"type": "content_block_delta", "index": 0, "delta": {"type": "signature_delta", "signature": "EqQBCgIYAhIM1gbcDa9GJwZA2b3hGgxBdjrkzLoky3dl1pkiMOYds..."}}

    Vollständige HTTP-Stream-Antwort

    Verwenden Sie die Client-SDKs bei Verwendung des Streaming-Modus. Wenn Sie jedoch eine direkte API-Integration erstellen, müssen Sie diese Ereignisse selbst verarbeiten.

    Eine Stream-Antwort besteht aus:

    1. Einem message_start-Ereignis
    2. Möglicherweise mehreren Content-Blöcken, von denen jeder enthält:
      • Ein content_block_start-Ereignis
      • Möglicherweise mehrere content_block_delta-Ereignisse
      • Ein content_block_stop-Ereignis
    3. Einem message_delta-Ereignis
    4. Einem message_stop-Ereignis

    Es können auch ping-Ereignisse über die gesamte Antwort verteilt sein. Weitere Informationen zum Format finden Sie unter Event types.

    Grundlegende Streaming-Anfrage

    curl https://api.anthropic.com/v1/messages \
         --header "anthropic-version: 2023-06-01" \
         --header "content-type: application/json" \
         --header "x-api-key: $ANTHROPIC_API_KEY" \
         --data \
    '{
      "model": "claude-opus-4-6",
      "messages": [{"role": "user", "content": "Hello"}],
      "max_tokens": 256,
      "stream": true
    }'
    Response
    event: message_start
    data: {"type": "message_start", "message": {"id": "msg_1nZdL29xx5MUA1yADyHTEsnR8uuvGzszyY", "type": "message", "role": "assistant", "content": [], "model": "claude-opus-4-6", "stop_reason": null, "stop_sequence": null, "usage": {"input_tokens": 25, "output_tokens": 1}}}
    
    event: content_block_start
    data: {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}}
    
    event: ping
    data: {"type": "ping"}
    
    event: content_block_delta
    data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "Hello"}}
    
    event: content_block_delta
    data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "!"}}
    
    event: content_block_stop
    data: {"type": "content_block_stop", "index": 0}
    
    event: message_delta
    data: {"type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence":null}, "usage": {"output_tokens": 15}}
    
    event: message_stop
    data: {"type": "message_stop"}
    

    Streaming-Anfrage mit Tool-Nutzung

    Tool-Nutzung unterstützt feingranulares Streaming für Parameterwerte. Aktivieren Sie es pro Tool mit eager_input_streaming.

    Diese Anfrage fordert Claude auf, ein Tool zu verwenden, um das Wetter zu melden.

      curl https://api.anthropic.com/v1/messages \
        -H "content-type: application/json" \
        -H "x-api-key: $ANTHROPIC_API_KEY" \
        -H "anthropic-version: 2023-06-01" \
        -d '{
          "model": "claude-opus-4-6",
          "max_tokens": 1024,
          "tools": [
            {
              "name": "get_weather",
              "description": "Get the current weather in a given location",
              "input_schema": {
                "type": "object",
                "properties": {
                  "location": {
                    "type": "string",
                    "description": "The city and state, e.g. San Francisco, CA"
                  }
                },
                "required": ["location"]
              }
            }
          ],
          "tool_choice": {"type": "any"},
          "messages": [
            {
              "role": "user",
              "content": "What is the weather like in San Francisco?"
            }
          ],
          "stream": true
        }'
    Response
    event: message_start
    data: {"type":"message_start","message":{"id":"msg_014p7gG3wDgGV9EUtLvnow3U","type":"message","role":"assistant","model":"claude-opus-4-6","stop_sequence":null,"usage":{"input_tokens":472,"output_tokens":2},"content":[],"stop_reason":null}}
    
    event: content_block_start
    data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}
    
    event: ping
    data: {"type": "ping"}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Okay"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":","}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" let"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"'s"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" check"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" the"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" weather"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" for"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" San"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" Francisco"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":","}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" CA"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":":"}}
    
    event: content_block_stop
    data: {"type":"content_block_stop","index":0}
    
    event: content_block_start
    data: {"type":"content_block_start","index":1,"content_block":{"type":"tool_use","id":"toolu_01T1x1fJ34qAmk2tNTrN7Up6","name":"get_weather","input":{}}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":""}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{\"location\":"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" \"San"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" Francisc"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"o,"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" CA\""}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":","}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" \"unit\": \"fah"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"renheit\"}"}}
    
    event: content_block_stop
    data: {"type":"content_block_stop","index":1}
    
    event: message_delta
    data: {"type":"message_delta","delta":{"stop_reason":"tool_use","stop_sequence":null},"usage":{"output_tokens":89}}
    
    event: message_stop
    data: {"type":"message_stop"}

    Streaming-Anfrage mit erweitertem Denken

    Diese Anfrage aktiviert erweitertes Denken mit Streaming, um Claudes schrittweise Überlegungen zu sehen.

    curl https://api.anthropic.com/v1/messages \
         --header "x-api-key: $ANTHROPIC_API_KEY" \
         --header "anthropic-version: 2023-06-01" \
         --header "content-type: application/json" \
         --data \
    '{
        "model": "claude-opus-4-6",
        "max_tokens": 20000,
        "stream": true,
        "thinking": {
            "type": "enabled",
            "budget_tokens": 16000
        },
        "messages": [
            {
                "role": "user",
                "content": "What is the greatest common divisor of 1071 and 462?"
            }
        ]
    }'
    Response
    event: message_start
    data: {"type": "message_start", "message": {"id": "msg_01...", "type": "message", "role": "assistant", "content": [], "model": "claude-opus-4-6", "stop_reason": null, "stop_sequence": null}}
    
    event: content_block_start
    data: {"type": "content_block_start", "index": 0, "content_block": {"type": "thinking", "thinking": "", "signature": ""}}
    
    event: content_block_delta
    data: {"type": "content_block_delta", "index": 0, "delta": {"type": "thinking_delta", "thinking": "I need to find the GCD of 1071 and 462 using the Euclidean algorithm.\n\n1071 = 2 × 462 + 147"}}
    
    event: content_block_delta
    data: {"type": "content_block_delta", "index": 0, "delta": {"type": "thinking_delta", "thinking": "\n462 = 3 × 147 + 21"}}
    
    event: content_block_delta
    data: {"type": "content_block_delta", "index": 0, "delta": {"type": "thinking_delta", "thinking": "\n147 = 7 × 21 + 0"}}
    
    event: content_block_delta
    data: {"type": "content_block_delta", "index": 0, "delta": {"type": "thinking_delta", "thinking": "\nThe remainder is 0, so GCD(1071, 462) = 21."}}
    
    event: content_block_delta
    data: {"type": "content_block_delta", "index": 0, "delta": {"type": "signature_delta", "signature": "EqQBCgIYAhIM1gbcDa9GJwZA2b3hGgxBdjrkzLoky3dl1pkiMOYds..."}}
    
    event: content_block_stop
    data: {"type": "content_block_stop", "index": 0}
    
    event: content_block_start
    data: {"type": "content_block_start", "index": 1, "content_block": {"type": "text", "text": ""}}
    
    event: content_block_delta
    data: {"type": "content_block_delta", "index": 1, "delta": {"type": "text_delta", "text": "The greatest common divisor of 1071 and 462 is **21**."}}
    
    event: content_block_stop
    data: {"type": "content_block_stop", "index": 1}
    
    event: message_delta
    data: {"type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence": null}}
    
    event: message_stop
    data: {"type": "message_stop"}

    Streaming-Anfrage mit Web-Such-Tool-Nutzung

    Diese Anfrage fordert Claude auf, das Web nach aktuellen Wetterinformationen zu durchsuchen.

    curl https://api.anthropic.com/v1/messages \
         --header "x-api-key: $ANTHROPIC_API_KEY" \
         --header "anthropic-version: 2023-06-01" \
         --header "content-type: application/json" \
         --data \
    '{
        "model": "claude-opus-4-6",
        "max_tokens": 1024,
        "stream": true,
        "tools": [
            {
                "type": "web_search_20250305",
                "name": "web_search",
                "max_uses": 5
            }
        ],
        "messages": [
            {
                "role": "user",
                "content": "What is the weather like in New York City today?"
            }
        ]
    }'
    Response
    event: message_start
    data: {"type":"message_start","message":{"id":"msg_01G...","type":"message","role":"assistant","model":"claude-opus-4-6","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":2679,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":3}}}
    
    event: content_block_start
    data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"I'll check"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" the current weather in New York City for you"}}
    
    event: ping
    data: {"type": "ping"}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"."}}
    
    event: content_block_stop
    data: {"type":"content_block_stop","index":0}
    
    event: content_block_start
    data: {"type":"content_block_start","index":1,"content_block":{"type":"server_tool_use","id":"srvtoolu_014hJH82Qum7Td6UV8gDXThB","name":"web_search","input":{}}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":""}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{\"query"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"\":"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" \"weather"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" NY"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"C to"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"day\"}"}}
    
    event: content_block_stop
    data: {"type":"content_block_stop","index":1 }
    
    event: content_block_start
    data: {"type":"content_block_start","index":2,"content_block":{"type":"web_search_tool_result","tool_use_id":"srvtoolu_014hJH82Qum7Td6UV8gDXThB","content":[{"type":"web_search_result","title":"Weather in New York City in May 2025 (New York) - detailed Weather Forecast for a month","url":"https://world-weather.info/forecast/usa/new_york/may-2025/","encrypted_content":"Ev0DCioIAxgCIiQ3NmU4ZmI4OC1k...","page_age":null},...]}}
    
    event: content_block_stop
    data: {"type":"content_block_stop","index":2}
    
    event: content_block_start
    data: {"type":"content_block_start","index":3,"content_block":{"type":"text","text":""}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":3,"delta":{"type":"text_delta","text":"Here's the current weather information for New York"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":3,"delta":{"type":"text_delta","text":" City:\n\n# Weather"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":3,"delta":{"type":"text_delta","text":" in New York City"}}
    
    event: content_block_delta
    data: {"type":"content_block_delta","index":3,"delta":{"type":"text_delta","text":"\n\n"}}
    
    ...
    
    event: content_block_stop
    data: {"type":"content_block_stop","index":17}
    
    event: message_delta
    data: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"input_tokens":10682,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":510,"server_tool_use":{"web_search_requests":1}}}
    
    event: message_stop
    data: {"type":"message_stop"}

    Fehlerwiederherstellung

    Claude 4.5 und früher

    Für Claude 4.5-Modelle und früher können Sie eine Streaming-Anfrage, die aufgrund von Netzwerkproblemen, Timeouts oder anderen Fehlern unterbrochen wurde, wiederherstellen, indem Sie von der Stelle aus fortfahren, an der der Stream unterbrochen wurde. Dieser Ansatz spart Ihnen die Neuverarbeitung der gesamten Antwort.

    Die grundlegende Wiederherstellungsstrategie umfasst:

    1. Erfassen Sie die Teilantwort: Speichern Sie alle Inhalte, die erfolgreich empfangen wurden, bevor der Fehler auftrat
    2. Konstruieren Sie eine Fortsetzungsanfrage: Erstellen Sie eine neue API-Anfrage, die die Teilantwort des Assistenten als Anfang einer neuen Assistentennachricht enthält
    3. Fortsetzen des Streaming: Fahren Sie fort, den Rest der Antwort von der Stelle aus zu empfangen, an der sie unterbrochen wurde

    Claude 4.6

    Für Claude 4.6-Modelle sollten Sie eine Benutzernachricht hinzufügen, die das Modell anweist, von der Stelle aus fortzufahren, an der es aufgehört hat. Zum Beispiel:

    Sample prompt
    Your previous response was interrupted and ended with [previous_response]. Continue from where you left off.

    Best Practices zur Fehlerwiederherstellung

    1. Verwenden Sie SDK-Funktionen: Nutzen Sie die integrierten Funktionen des SDK für Nachrichtenakkumulation und Fehlerbehandlung
    2. Behandeln Sie Inhaltstypen: Beachten Sie, dass Nachrichten mehrere Inhaltsblöcke enthalten können (text, tool_use, thinking). Tool-Use- und Extended-Thinking-Blöcke können nicht teilweise wiederhergestellt werden. Sie können das Streaming vom neuesten Textblock aus fortsetzen.