events-and-streaming changedmanaged-agents/events-and-streaming
Nearest release: v2.1.304, published under an hour after this site recorded the change. Shown because the two are within 24 hours of each other. Nothing here says the release caused the edit.
Recorded here
Lines+23added
Lines−161removed
From line
27
where the diff opens
First seen
14 Aug 2026
this site's first read of the page
Recorded edits29to this page, all time
The whole hunk
from line 27, old and new numbered
/
from line 27
2727
2828Stream events from the session to receive real-time updates as the agent works. A stream delivers only the events emitted after it opens, so open the stream before you send events to avoid a race condition.
2929
30<CodeGroup exclude="shell:CLI">
30<CodeGroup>
3131 ```bash cURL
32 # Open the stream first, then send the user message
33 exec {stream}< <(
34 curl --fail-with-body -sS -N \
35 "https://api.anthropic.com/v1/sessions/$SESSION_ID/events/stream?beta=true" \
36 -H "x-api-key: $ANTHROPIC_API_KEY" \
37 -H "anthropic-version: 2023-06-01" \
38 -H "anthropic-beta: managed-agents-2026-04-01" \
39 -H "content-type: application/json" \
40 -H "accept: text/event-stream"
41 )
42
43 curl --fail-with-body -sS \
44 "https://api.anthropic.com/v1/sessions/$SESSION_ID/events?beta=true" \
32 # Open the stream before you send the first event: only later events are delivered.
33 curl --fail-with-body -sS -N \
34 "https://api.anthropic.com/v1/sessions/$SESSION_ID/events/stream?beta=true" \
4535 -H "x-api-key: $ANTHROPIC_API_KEY" \
4636 -H "anthropic-version: 2023-06-01" \
4737 -H "anthropic-beta: managed-agents-2026-04-01" \
48 -H "content-type: application/json" \
49 -d '{
50 "events": [
51 {
52 "type": "user.message",
53 "content": [{"type": "text", "text": "Summarize the repo README"}]
54 }
55 ]
56 }' >/dev/null
38 -H "accept: text/event-stream"
39 ```
5740
58 while IFS= read -r -u "$stream" event_line; do
59 [[ $event_line == data:* ]] || continue
60 event_json=${event_line#data: }
61 case $(jq -r '.type' <<<"$event_json") in
62 agent.message)
63 jq -j '.content[] | select(.type == "text") | .text' <<<"$event_json"
64 ;;
65 session.status_idle)
66 break
67 ;;
68 session.error)
69 printf '\n[Error: %s]\n' "$(jq -r '.error.message // "unknown"' <<<"$event_json")"
70 break
71 ;;
72 esac
73 done
74 exec {stream}<&-
41 ```bash CLI
42 # Open the stream before you send the first event: only later events are delivered.
43 ant beta:sessions:events stream --session-id "$SESSION_ID"
7544 ```
7645
77 <CodeGroupItem language="CLI">
78 This workflow needs more logic than a single shell command. Use the [Claude SDK](https://platform.claude.com/docs/en/cli-sdks-libraries/overview) to run it.
79 </CodeGroupItem>
80
8146 ```python Python
8247 # Open the stream first, then send the user message
8348 with client.beta.sessions.events.stream(session.id) as stream:
from line 295
3302952. [List the full event history](https://platform.claude.com/docs/en/managed-agents/events-and-streaming#list-past-events) to seed a set of seen event IDs.
3312963. Tail the live stream, skipping any events already returned by the history list.
332297
333<CodeGroup exclude="shell:CLI">
334 ```bash cURL
335 exec {stream}< <(
336 curl --fail-with-body -sS -N \
337 "https://api.anthropic.com/v1/sessions/$SESSION_ID/events/stream?beta=true" \
338 -H "x-api-key: $ANTHROPIC_API_KEY" \
339 -H "anthropic-version: 2023-06-01" \
340 -H "anthropic-beta: managed-agents-2026-04-01" \
341 -H "content-type: application/json" \
342 -H "accept: text/event-stream"
343 )
298<CodeGroup exclude="shell:cURL, shell:CLI">
299 <CodeGroupItem language="cURL">
300 This workflow needs more logic than a single shell command. Use the [Claude SDK](https://platform.claude.com/docs/en/cli-sdks-libraries/overview) to run it.
301 </CodeGroupItem>
344302
345 # Stream is open and buffering. List history before tailing live.
346 declare -A seen_event_ids
347 while IFS= read -r event_id; do
348 seen_event_ids[$event_id]=1
349 done < <(
350 curl --fail-with-body -sS \
351 "https://api.anthropic.com/v1/sessions/$SESSION_ID/events?beta=true" \
352 -H "x-api-key: $ANTHROPIC_API_KEY" \
353 -H "anthropic-version: 2023-06-01" \
354 -H "anthropic-beta: managed-agents-2026-04-01" \
355 -H "content-type: application/json" | jq -r '.data[].id'
356 )
357
358 # Tail live events, skipping anything already seen
359 while IFS= read -r -u "$stream" event_line; do
360 [[ $event_line == data:* ]] || continue
361 event_json=${event_line#data: }
362 event_id=$(jq -r '.id' <<<"$event_json")
363 [[ -n ${seen_event_ids[$event_id]+seen} ]] && continue
364 seen_event_ids[$event_id]=1
365 case $(jq -r '.type' <<<"$event_json") in
366 agent.message)
367 jq -j '.content[] | select(.type == "text") | .text' <<<"$event_json"
368 ;;
369 session.status_idle)
370 break
371 ;;
372 esac
373 done
374 exec {stream}<&-
375 ```
376
377303 <CodeGroupItem language="CLI">
378304 This workflow needs more logic than a single shell command. Use the [Claude SDK](https://platform.claude.com/docs/en/cli-sdks-libraries/overview) to run it.
379305 </CodeGroupItem>
from line 694
768694
769695The following example answers each call as it arrives:
770696
771<CodeGroup exclude="shell:CLI">
772 ```bash cURL
773 exec {stream_fd}< <(curl --fail-with-body -sS -N \
774 "https://api.anthropic.com/v1/sessions/$SESSION_ID/events/stream?beta=true" \
775 -H "x-api-key: $ANTHROPIC_API_KEY" \
776 -H "anthropic-version: 2023-06-01" \
777 -H "anthropic-beta: managed-agents-2026-04-01" \
778 -H "content-type: application/json" \
779 -H "accept: text/event-stream")
697<CodeGroup exclude="shell:cURL, shell:CLI">
698 <CodeGroupItem language="cURL">
699 This workflow needs more logic than a single shell command. Use the [Claude SDK](https://platform.claude.com/docs/en/cli-sdks-libraries/overview) to run it.
700 </CodeGroupItem>
780701
781 while IFS= read -r -u "$stream_fd" line; do
782 [[ $line == data:* ]] || continue
783 event_json="${line#data: }"
784 case $(jq -r '.type' <<<"$event_json") in
785 agent.custom_tool_use)
786 # Execute the tool and send the result back
787 result=$(call_tool "$(jq -r '.name' <<<"$event_json")" "$(jq -c '.input' <<<"$event_json")")
788 jq --arg result "$result" \
789 '{events: [{type: "user.custom_tool_result", custom_tool_use_id: .id, content: [{type: "text", text: $result}]}]}' <<<"$event_json" |
790 curl --fail-with-body -sS \
791 "https://api.anthropic.com/v1/sessions/$SESSION_ID/events?beta=true" \
792 -H "x-api-key: $ANTHROPIC_API_KEY" \
793 -H "anthropic-version: 2023-06-01" \
794 -H "anthropic-beta: managed-agents-2026-04-01" \
795 -H "content-type: application/json" \
796 -d @-
797 ;;
798 session.status_idle)
799 if [[ $(jq -r '.stop_reason.type' <<<"$event_json") == end_turn ]]; then
800 break
801 fi
802 ;;
803 esac
804 done
805 exec {stream_fd}<&-
806 ```
807
808702 <CodeGroupItem language="CLI">
809703 This workflow needs more logic than a single shell command. Use the [Claude SDK](https://platform.claude.com/docs/en/cli-sdks-libraries/overview) to run it.
810704 </CodeGroupItem>
from line 925
1031925
1032926The following example approves every pending call:
1033927
1034<CodeGroup exclude="shell:CLI">
1035 ```bash cURL
1036 exec {stream_fd}< <(curl --fail-with-body -sS -N \
1037 "https://api.anthropic.com/v1/sessions/$SESSION_ID/events/stream?beta=true" \
1038 -H "x-api-key: $ANTHROPIC_API_KEY" \
1039 -H "anthropic-version: 2023-06-01" \
1040 -H "anthropic-beta: managed-agents-2026-04-01" \
1041 -H "content-type: application/json" \
1042 -H "accept: text/event-stream")
1043
1044 while IFS= read -r -u "$stream_fd" line; do
1045 [[ $line == data:* ]] || continue
1046 event_json="${line#data: }"
1047 stop_reason=$(jq -r 'select(.type == "session.status_idle") | .stop_reason.type // empty' <<<"$event_json")
1048 case "$stop_reason" in
1049 requires_action)
1050 while IFS= read -r event_id; do
1051 # Approve the pending tool call
1052 jq -n --arg id "$event_id" \
1053 '{events: [{type: "user.tool_confirmation", tool_use_id: $id, result: "allow"}]}' |
1054 curl --fail-with-body -sS \
1055 "https://api.anthropic.com/v1/sessions/$SESSION_ID/events?beta=true" \
1056 -H "x-api-key: $ANTHROPIC_API_KEY" \
1057 -H "anthropic-version: 2023-06-01" \
1058 -H "anthropic-beta: managed-agents-2026-04-01" \
1059 -H "content-type: application/json" \
1060 -d @-
1061 done < <(jq -r '.stop_reason.event_ids[]' <<<"$event_json")
1062 ;;
1063 end_turn)
1064 break
1065 ;;
1066 esac
1067 done
1068 exec {stream_fd}<&-
1069 ```
928<CodeGroup exclude="shell:cURL, shell:CLI">
929 <CodeGroupItem language="cURL">
930 This workflow needs more logic than a single shell command. Use the [Claude SDK](https://platform.claude.com/docs/en/cli-sdks-libraries/overview) to run it.
931 </CodeGroupItem>
1070932
1071933 <CodeGroupItem language="CLI">
1072934 This workflow needs more logic than a single shell command. Use the [Claude SDK](https://platform.claude.com/docs/en/cli-sdks-libraries/overview) to run it.
No line in this hunk matches that.