0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LLMサーバをStreaming対応した実装メモ

0
Last updated at Posted at 2026-02-20

はじめに

現在、RAGベースのMCPサーバをAWS上で構築しており、APIGWのタイムアウト上限を引き上げるために Streaming Response へ対応しました。

備忘録として対応内容を簡単に記載いたします。


背景

開発していたのは以下のような構成のLLMサーバです。

  • API Gateway(REST API)
  • AWS Lambda
  • Lambda Web Adapter
  • FastAPI / FastMCP
  • Bedrock Claude(LLM)

もともとは通常の統合レスポンスでしたが、
検索処理が重く、従来のタイムアウトである29秒を超過してしまうことが多々あったため、Streaming Response に変更しました。


やりたかったこと

やりたかった構成はシンプルです。

Client → API Gateway → Lambda(Web Adapter) → LLM
                                 ↓
                           Streaming Response

LLMのトークン生成に合わせてフロントへレスポンスを流します。


結論

Streaming対応で重要だったポイントは下記3つになります。

  1. API Gateway Integration の uri を Lambda ARN に変更する
  2. responseTransferMode を STREAM にする
  3. Lambda の環境変数 AWS_LWA_INVOKE_MODE=response_stream を設定

この3つが揃っていないと正常にデプロイ、動作しませんでした。


実装内容

① API Gateway 統合設定

OpenAPI定義で以下を設定します。

x-amazon-apigateway-integration:
  type: aws_proxy
  httpMethod: POST
  responseTransferMode: STREAM
  timeoutInMillis: 180000
  uri: arn:aws:apigateway:${AWS::Region}:lambda:path/2021-11-15/functions/${StreamingLambdaFunction.Arn}/response-streaming-invocations

② Lambda Web Adapter 設定

Lambda 側の環境変数に以下を追加します。

Environment:
  Variables:
    AWS_LWA_INVOKE_MODE: response_stream

上記変数がないとエラーになりました。


③ SAM Template 側

Globals または Function 定義に Web Adapter Layer を追加している前提です。

Layers:
  - !Ref CommonLayer
  - arn:aws:lambda:${AWS::Region}:xxxxxxxxxxxx:layer:AWS-Parameters-and-Secrets-Lambda-Extension:20

Web Adapterを使用してFastAPIを直接公開しています。


まとめ

LLMサーバをStreaming対応する場合は、

  • responseTransferMode
  • response-streaming-invocations
  • AWS_LWA_INVOKE_MODE

この3点をまず確認するのがおすすめです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?