← 뒤로

AWS Bedrock + Claude SSE 스트리밍 구현

조회 1 4/5/2026
AWS BedrockClaudeSSEStreamingNode.js

Bedrock SSE 스트리밍

AWS Bedrock에서 Claude API를 실시간 스트리밍으로 연동합니다.

왜 스트리밍?

서버 구현

const { BedrockRuntimeClient, ConverseStreamCommand } = require('@aws-sdk/client-bedrock-runtime');

const client = new BedrockRuntimeClient({ region: 'us-east-1' });

app.post('/api/interpret', async (req, res) => {
  res.setHeader('Content-Type', 'text/event-stream');
  res.setHeader('Cache-Control', 'no-cache');

  const command = new ConverseStreamCommand({
    modelId: 'global.anthropic.claude-sonnet-4-6',
    messages: [{ role: 'user', content: [{ text: req.body.prompt }] }],
    inferenceConfig: { temperature: 0.2 }
  });

  const response = await client.send(command);
  for await (const event of response.stream) {
    if (event.contentBlockDelta) {
      res.write(`data: ${JSON.stringify({ text: event.contentBlockDelta.delta.text })}\n\n`);
    }
  }
  res.write('data: [DONE]\n\n');
  res.end();
});

클라이언트

const evtSource = new EventSource('/api/interpret');
evtSource.onmessage = (e) => {
  if (e.data === '[DONE]') return evtSource.close();
  const { text } = JSON.parse(e.data);
  outputDiv.textContent += text;
};