• Projects
  • Service
  • About
  • branding.bz
  • Podcast
  • Tips
  • FAQ
  • Recruit
  • Download
  • Contact
  • branding.bz(ブランド構築SaaS)
  • DESIGN NOW(デザインメディア)
  • X
  • LinkedIn
  • Spotify
  • Facebook

213-0011 神奈川県川崎市高津区久本3-6-7-303

© 2026 ID INC. All rights reserved

claude-skills/スキル
SKILLOfficialdevelopment

output-dev-llm-streaming

プラグイン
outputai
ソース
GitHub で見る ↗
説明

LLM テキストストリーミング(大規模言語モデルの出力を少しずつ送信する機能)を Output ワークフローステップに実装する際に、generateTextWithStreaming、Agent.generateWithStreaming、streamText、または Agent.stream を使用します。 次のような場合に使用: - トークン進捗状況(処理の段階的な進行状況)の追加 - onChunk コールバック(受信時に実行される処理)の設定 - streamText の onEnd/onError と Temporal リトライ(再試行)の組み合わせでのエラー処理

原文を表示

Implement LLM text streaming in Output workflow steps with generateTextWithStreaming, Agent.generateWithStreaming, streamText, or Agent.stream. Use when adding token progress, onChunk callbacks, or handling streamText onEnd/onError with Temporal retries.

ユースケース
  • トークン進捗状況を追加したい
  • チャンク受信時に処理を実行したい
  • ストリーミング出力のエラー処理をしたい
本文(日本語訳)

LLM テキストストリーミング

このスキルを使用する場合

  • LLM(大規模言語モデル)を使った処理の中で、進行状況をトークン(単語単位の情報)やチャンク(小分けにした情報)で表示したい
  • 生成完了まで待つか、流れてくる途中結果に直接アクセスするか選択したい
  • streamText() や Agent.stream() で onChunk(到着ごとに実行される処理)や onEnd(終了時)/ onError(エラー時)を使いたい
  • ストリーミング処理の失敗時に Temporal(ワークフロー管理システム)の活動を再試行したい
  • エージェント(AIアシスタント)の応答をストリーミング表示したり、会話履歴を保存したい

API を選ぶ

必要な機能 使う API
完全な結果を一度に取得 generateText()
完全な結果+進行状況の表示 generateTextWithStreaming()
ストリーミングデータに直接アクセス streamText()
エージェントの完全な結果+進行状況の表示 Agent.generateWithStreaming()
エージェントのストリームに直接アクセス Agent.stream()

ワークフロー内のステップ(処理単位)では、進行状況の表示で十分な場合は generateTextWithStreaming() や Agent.generateWithStreaming() を優先してください。これらは内部でストリームを処理し、generateText() や Agent.generate() と同様に完全な結果を返します。また、提供元・通信・中止エラーが発生した場合は例外を発生させるため、Temporal がエラー時の再試行ポリシーを適用できます。

streamText() と Agent.stream() は、ストリーミングデータを詳細に制御する必要があるコード向けに残されています。

generateTextWithStreaming()

import { generateTextWithStreaming } from '@outputai/llm';

const result = await generateTextWithStreaming( {
  prompt: 'draft@v1',
  variables: { topic },
  onChunk( { chunk } ) {
    if ( chunk.type === 'text-delta' ) {
      process.stdout.write( chunk.text );
    }
  }
} );

return result.result;

結果には generateText() と同じ完全な情報が含まれます。result、text、output、usage(利用量)、finishReason(終了理由)、cost(費用)などです。aiSdk.Output.* で指定した構造化出力(決まった形式の出力)は result.output で取得できます。

Agent.generateWithStreaming()

const result = await agent.generateWithStreaming( {
  onChunk( { chunk } ) {
    if ( chunk.type === 'text-delta' ) {
      process.stdout.write( chunk.text );
    }
  }
} );

generateWithStreaming() はエージェントの完全な応答を返し、エージェントが messageStore(会話履歴保存機能)を持っている場合、自動的にメッセージを保存します。

ストリーミングの直接的なエラー処理

AI SDK のストリーミングでは、提供元や通信エラーは onError を通じて伝えられます。textStream を処理しながら反復しても、元のエラーが確実に発生するわけではありません。ワークフローステップで streamText() を使う場合は、エラーを捕捉してから処理完了後に発生させてください。

import { streamText } from '@outputai/llm';

const captured: { error: unknown } = { error: null };
const result = streamText( {
  prompt: 'draft@v1',
  variables: { topic },
  onError( { error } ) {
    captured.error = error;
  }
} );

const chunks: string[] = [];
for await ( const chunk of result.textStream ) {
  chunks.push( chunk );
}

if ( captured.error ) {
  throw captured.error;
}

return chunks.join( '' );

onError を登録しながら捕捉したエラーを発生させないと、ステップが正常に完了してしまい、Temporal が再試行できなくなります。完了を示す属性を待つと、元の提供元エラーではなく、汎用的な「出力なし」エラーが起こることもあります。

Agent.stream() は finishReason が 'error' でない場合、onEnd でラップされた会話メッセージを保存します。完全な保存済み応答が必要な場合は Agent.generateWithStreaming() を使ってください。

ストリーミング呼び出しの引数: prompt、promptDir、variables、tools、output、toolChoice、stopWhen、abortSignal、そして onChunk(generateTextWithStreaming の場合)または onChunk / onEnd / onError(streamText の場合)。エージェントメソッド: messages、abortSignal、toolChoice、そして前述のストリーミングコールバック。

ルール

  • Temporal ステップでは、直接的なストリーム制御が必要でない限り、完全結果を返すストリーミング API を優先してください。
  • streamText() でステップを失敗させるために onError だけに頼らないでください。
  • キャプチャしたエラーは、ストリーム処理が終わった後にのみ発生させてください。
  • onChunk 内の副作用(その他の処理)を制限してください。1トークンごとに Temporal シグナル(信号)を送ると、シグナルごとに履歴イベントが作成されるため、高頻度の更新はまとめてください。
  • streamText() や Agent.stream() を非推奨として説明しないでください。

関連スキル

  • output-dev-step-function - LLM 呼び出しを Temporal ステップの中に配置する
  • output-dev-agent-class - 再利用可能なエージェントを構築・使用する
  • output-dev-prompt-file - 生成用のプロンプトファイルを作成する
  • output-error-try-catch - ステップとワークフローのエラーを処理する
原文(English)を表示

LLM Text Streaming

When to Use This Skill

  • Adding token or chunk progress to an LLM-powered step
  • Choosing between completed generation and direct stream access
  • Using onChunk, or onEnd / onError on streamText() / Agent.stream()
  • Making stream failures trigger Temporal activity retries
  • Streaming Agent responses or persisting streamed conversations

Choose the API

Need Use
Complete single-shot result generateText()
Complete result plus onChunk progress generateTextWithStreaming()
Direct access to textStream or stream streamText()
Complete Agent result plus onChunk progress Agent.generateWithStreaming()
Direct access to the Agent stream Agent.stream()

In workflow steps, prefer generateTextWithStreaming() or Agent.generateWithStreaming() when onChunk progress is sufficient. They consume the stream internally, return complete results like generateText() or Agent.generate(), and reject on provider, transport, or abort errors. Rejection allows Temporal to record the failed activity attempt and apply the step retry policy.

streamText() and Agent.stream() remain supported for code that needs direct control over stream consumption.

generateTextWithStreaming()

import { generateTextWithStreaming } from '@outputai/llm';

const result = await generateTextWithStreaming( {
  prompt: 'draft@v1',
  variables: { topic },
  onChunk( { chunk } ) {
    if ( chunk.type === 'text-delta' ) {
      process.stdout.write( chunk.text );
    }
  }
} );

return result.result;

The result has the same complete response fields as generateText(), including result, text, output, usage, finishReason, and cost. Structured output passed with aiSdk.Output.* is available through result.output.

Agent.generateWithStreaming()

const result = await agent.generateWithStreaming( {
  onChunk( { chunk } ) {
    if ( chunk.type === 'text-delta' ) {
      process.stdout.write( chunk.text );
    }
  }
} );

generateWithStreaming() returns a complete Agent response and automatically stores messages when the Agent has a messageStore.

Direct stream error handling

AI SDK streaming delivers provider and transport failures through onError. Iterating textStream does not reliably throw the original error. When using streamText() in a workflow step, capture the error and throw it after consumption:

import { streamText } from '@outputai/llm';

const captured: { error: unknown } = { error: null };
const result = streamText( {
  prompt: 'draft@v1',
  variables: { topic },
  onError( { error } ) {
    captured.error = error;
  }
} );

const chunks: string[] = [];
for await ( const chunk of result.textStream ) {
  chunks.push( chunk );
}

if ( captured.error ) {
  throw captured.error;
}

return chunks.join( '' );

Registering onError without throwing the captured error can let the step return an empty successful result, preventing Temporal from retrying it. Awaiting a completion property may also produce a generic no-output error instead of the original provider error.

Agent.stream() stores conversation messages in its wrapped onEnd when finishReason is not 'error'. Use Agent.generateWithStreaming() when a complete stored response meets the requirement.

Streaming call arguments: prompt, promptDir, variables, tools, output, toolChoice, stopWhen, abortSignal, plus onChunk (generateTextWithStreaming) or onChunk / onEnd / onError (streamText). Agent methods: messages, abortSignal, toolChoice, plus those same stream callbacks.

Rules

  • Prefer the completed streaming APIs in Temporal steps unless direct stream access is required.
  • Do not rely on onError alone to fail a step using streamText().
  • Throw the captured error only after stream consumption finishes.
  • Keep onChunk side effects bounded. A Temporal signal per token creates a history event per signal, so batch high-frequency updates.
  • Do not describe streamText() or Agent.stream() as deprecated.

Related Skills

  • output-dev-step-function - Put LLM calls inside Temporal activity steps
  • output-dev-agent-class - Construct and use reusable Agents
  • output-dev-prompt-file - Create prompt files for generation
  • output-error-try-catch - Handle step and workflow failures

原文・著作権は Anthropic および各プラグイン作者に帰属します。日本語訳は Claude API による自動翻訳です。