Lesson 38.5 · 5 of 7 · 4 min read · Reviewed 24 Nov 2026
Long-running tasks, streaming and push updates
By the end, you can
- Handle tasks that take minutes or days.
- Choose between polling, streaming and push notifications.
Some tasks finish in seconds. Others, like a background check or a supplier quote, take hours or days.
Three ways to get updates
| Method | How it works | Good for |
|---|---|---|
| Polling | Client asks for the task’s state now and then | Simple clients, moderate durations |
| Streaming | Server sends events over an open connection (Server-Sent Events) | Tasks of seconds to minutes, live progress in a UI |
| Push notifications | Server calls the client’s webhook when the state changes | Hours or days; clients that can’t hold a connection |
An agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. card says which of these an agent supports.
Design tips
- Make tasks resumable. Store the task id so you can check its state after a restart or a dropped streamStreaming: Receiving a response as a series of server-sent events while it is generated, instead of waiting for the whole reply..
- Set timeouts and budgets on your side. A remote agent that never finishes shouldn’t block yours forever (42.3).
- Verify webhook calls. Check that a push really comes from the agent you expect (38.6).
Key points
- Short tasks can return in one response; long ones need progress updates.
- Streaming sends updates over an open connection while the client waits.
- Push notifications call the client's webhook when something changes, so nobody has to stay connected.
Poll a long task with a deadline
Follow a long-running remote task safely: poll with backoff, stop at your own deadline, and resume from a stored task id.
You will have: A poller that prints each check, gives up cleanly at the deadline, and later resumes the same task to completion.
Steps
- Read the simulated remote agent: a task finishes after a fixed amount of simulated time.
- Write poll(task_id, deadline) that checks the state, then waits 1, 2, 4, 8 seconds and so on, capped at 30.
- Use a simulated clock (a number you add to), not real sleeping, so the lab runs instantly.
- Stop at the deadline with a 'still working, will check later' result, and save the task id.
- Resume later from the saved id and poll until completed. Count the polls.
Done when
- The first run stops at its 60-second deadline without the task finishing.
- The resumed run reaches completed using the stored task id.
- Backoff waits never exceed 30 seconds.
Open in the Playground No API key needed.
Starter code (Python)
clock = {"now": 0}
remote = {"task-88": {"finishes_at": 150}} # simulated: the task completes 150 s after it starts
def get_state(task_id):
return "completed" if clock["now"] >= remote[task_id]["finishes_at"] else "working"
def poll(task_id, deadline):
wait, polls = 1, 0
while True:
polls += 1
state = get_state(task_id)
print(f"t={clock['now']:>3}s {task_id} {state}")
if state == "completed":
return state, polls
# TODO: stop if the next wait would pass the deadline
clock["now"] += wait
# TODO: double the wait, capped at 30
if polls > 50:
return "gave up", polls
saved = {"task_id": "task-88"}
print(poll(saved["task_id"], deadline=60))
Show a solution
clock = {"now": 0}
remote = {"task-88": {"finishes_at": 150}} # simulated: the task completes 150 s after it starts
def get_state(task_id):
return "completed" if clock["now"] >= remote[task_id]["finishes_at"] else "working"
def poll(task_id, deadline):
wait, polls = 1, 0
while True:
polls += 1
state = get_state(task_id)
print(f"t={clock['now']:>3}s {task_id} {state}")
if state == "completed":
return state, polls
if clock["now"] + wait > deadline:
return "still working, will check later", polls
clock["now"] += wait
wait = min(wait * 2, 30)
saved = {"task_id": "task-88"} # stored so a restart can resume
print("run 1:", poll(saved["task_id"], deadline=60))
clock["now"] = 120 # later, after a restart
print("run 2:", poll(saved["task_id"], deadline=400))