In the previous lessons we've been calling completion() once at a time. Now let's see what happens when we fire two calls simultaneously.
A loaded model is one native context under the hood: one KV-cache, one decode loop. Two completions on the same model cannot literally run at the same time. The SDK uses a per-(kind, modelId) FIFO admission queue, so the second request waits its turn instead of being rejected with RequestRejectedByPolicyError.
The trick is calling completion() twice in the same tick before awaiting either.
First we'd need to fire both completion() calls in the same tick, no await between. The two back-to-back calls look as follows:
const r1 = completion({ modelId, history, stream: false, captureThinking: true });
const r2 = completion({ modelId, history, stream: false, captureThinking: true });Next, we'd use the canonical pattern to await the results. Promise.all is the surface; the wait over both resolves to:
const [text1, text2] = await Promise.all([r1.text, r2.text]);
console.log(`▸ req-A: ${text1}`);
console.log(`▸ req-B: ${text2}`);
console.log(`▸ Both completed.`);Both completion() calls fire synchronously, so both end up "in flight" against the SDK's queue. Promise.all then waits for them. On the same model, the queue runs them in order. On different models, the per-model key means they run in parallel.
Note: if you
awaitthe first call before the secondcompletion(), the second call never sees contention and the queue is bypassed. Fire both first, await together.
completion({ modelId, history, stream: false, captureThinking: true }) twice in a row without awaiting either, assigning each to a variable.Promise.all([r1.text, r2.text]) and log the result of each with a label like req-A and req-B.▸ Both completed. so the output has a final line.$ Run your code to see results
$