Skip to content

Heartbeat Vercel AI SDK streamed model calls only on the timer - #516

Merged
campoy merged 4 commits into
mainfrom
feat/vercel-ai-sdk-timer-only-heartbeat
Nov 14, 2026
Merged

campoy merged 4 commits into
mainfrom
feat/vercel-ai-sdk-timer-only-heartbeat

Conversation

@campoy

@campoy campoy commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

What was changed

invokeModelStreaming no longer heartbeats on every stream part. Its timer (heartbeatUntilDisposed) sends all its heartbeats, every half of the timeout, starting half the timeout in.

  • A streamed call shorter than half the timeout (5 seconds with the 10-second default) sends no heartbeat.
  • With heartbeatTimeout: 0, it sends no heartbeat at all. Before, it still heartbeated on each part.
  • The README now says both, and the timer's comment says it sends the only heartbeats.

Why?

#491 sets one rule for every plugin: heartbeats come only from the timer. The per-part heartbeat broke it. The SDK sends the first heartbeat at once, so every streamed call cost a heartbeat, which in Temporal Cloud is an Action, even a call that ended in a second.

I checked it with a streamed call that sends 11 parts at once and then waits 3 seconds. 1.5 seconds in, I read the pending Activity from DescribeWorkflowExecution:

main:        1.5s in: activity=invokeModelStreaming lastHeartbeatTime=set
this branch: 1.5s in: activity=invokeModelStreaming lastHeartbeatTime=none

Does a slow stream still heartbeat in time? Yes. The timer doesn't wait for parts, and the slow-provider tests pass: a stream whose first part takes 2 seconds, with a 1-second heartbeat timeout, still completes.

Does this break anything? Not replay. Heartbeats happen inside the Activity, and History doesn't record them, so the Workflow's commands don't change.

Checklist

  1. Closes [Feature Request] Heartbeat Vercel AI SDK streamed model calls only on the timer #515

  2. How was this tested:

    • invokeModelStreaming publishes every part and assembles the final result now runs with the 10-second default heartbeat timeout and counts heartbeats. Its 12 parts send none. Without the change it fails:
      Difference (- actual, + expected):
      - 12
      + 0
      
    • The three Model activity (<call>) heartbeats while the provider is slow tests pass with and without the change.
    • invokeModelStreaming sends no heartbeat when heartbeatTimeout is 0 stalls the provider for 100 ms with heartbeatTimeout: 0, and checks that no heartbeat goes out. If the timer's guard skips only an undefined timeout, it sends 87 and the test fails. On main, a 12-part stream with heartbeatTimeout: 0 sends 12 heartbeats, one per part.
    • Locally, make test (87 passed, 1 skipped), make lint and the conventions check pass, on Node 24 with Temporal TypeScript SDK 1.24.0.
  3. Any docs updates needed?
    The README is updated here. Release notes can say that a streamed call shorter than half its heartbeat timeout no longer sends a heartbeat.

invokeModelStreaming heartbeated on every stream part, besides its timer. The
SDK sends the first heartbeat at once, so even a short streamed call cost a
heartbeat, which in Temporal Cloud is an Action. With heartbeatTimeout: 0, it
still heartbeated on each part.

Remove the per-part heartbeat. The timer already heartbeats every half of the
timeout while the call runs, starting half the timeout in, so a call shorter
than that sends none, and 0 turns heartbeats off.

Part of #491.
@campoy
campoy marked this pull request as ready for review November 13, 2026 03:57
@campoy
campoy requested a review from a team as a code owner November 13, 2026 03:57
Comment thread typescript/vercel-ai-sdk/README.md Outdated
Comment on lines +70 to +72
set one in the provider options. They heartbeat every half of that timeout while
they run, so a lost Worker is noticed, and a cancellation request reaches the
Activity, within about that time. A call shorter than half the timeout sends no

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could a reader take "that time" as half the timeout? Both limits are closer to the full timeout. A lost Worker is noticed one timeout after its last heartbeat. With the default, a cancellation request can wait about 8 seconds, because the SDK sends at most one heartbeat every 0.8 × the timeout. Your numbers in #491 show up to 7 seconds. I'd suggest "within about that timeout".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it reads that way. I changed it to "within about that timeout".

Comment on lines +95 to +96
// The heartbeat timeout the plugin schedules by default.
const env = new MockActivityEnvironment({ heartbeatTimeoutMs: 10_000 }, { client: recordingClient(published) });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You checked heartbeatTimeout: 0 by hand. Is it worth a test for it? On this instant stream it would add nothing, because the removed call never read the timeout. A stream that stalls for a short time would guard the early return in heartbeatUntilDisposed. I removed that return locally. A 350 ms stall with heartbeatTimeoutMs: 0 then sent 274 heartbeats, and the suite still passed. I'm OK with a follow-up for it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it here. The provider stalls for 100 ms with heartbeatTimeoutMs: 0, and the test checks that no heartbeat goes out.

To see it fail, I changed the guard to skip only an undefined timeout (just deleting the line does not compile). The test then fails:

Difference (- actual, + expected):
- 87
+ 0

"Within about that time" read as half the timeout, but a lost Worker is
noticed one timeout after its last heartbeat, and a cancellation can wait
up to about 0.8 of the timeout, because the SDK throttles heartbeats.
The early return in heartbeatUntilDisposed had no test. With a guard that
only skips an undefined timeout, a 100 ms stall sends 87 heartbeats and
the new test fails.
@campoy
campoy merged commit 6348259 into main Nov 14, 2026
15 checks passed
@campoy
campoy deleted the feat/vercel-ai-sdk-timer-only-heartbeat branch November 14, 2026 02:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Heartbeat Vercel AI SDK streamed model calls only on the timer

2 participants