{
  "id": 18100198,
  "title": "Streaming Claude Tokens with Fastify on Lambda: A Step‑By‑Step Guide Explained Simply",
  "url": "https://urgent.news/2026/11/04/streaming-claude-tokens-with-fastify-on-lambda-a-step-by-step-guide",
  "topic": "ai",
  "section": "AI",
  "published": "2026-11-04T09:40:12.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/dineshgowtham/streaming-claude-tokens-with-fastify-on-lambda-a-step-by-step-guide-explained-simply-4ini"
  },
  "original_language": "en",
  "account": "Developers often believe they require powerful frameworks to manage streaming responses from language models, but Fastify’s minimalistic core can efficiently proxy Claude’s token stream with minimal overhead. This guide demonstrates how to transform a Fastify service into a real-time chat endpoint that transmits tokens directly to the browser.\n\nFastify outperforms Express in streaming LLM responses. When you prompt a language model like Claude, it generates text in token form, one token at a time. To create a \"live-typing\" experience, each token must be forwarded to the browser immediately upon arrival. Express, a widely-used Node.js framework, incorporates numerous middleware layers to enhance flexibility. While these layers are beneficial, each additional function call introduces minuscule latency. For a service handling dozens of tokens per second, this cumulative latency becomes noticeable. Conversely, Fastify prioritizes performance. Its core is streamlined, comprising solely the HTTP server and routing logic. It natively supports async/await, eliminating the necessity for callbacks that could block the event loop. Leveraging the ultra-fast ultra-http library under the hood, Fastify facilitates rapid transmission of data from the model to the client, akin to a high-speed conveyor belt. In contrast, Express operates like a congested hallway where the same package (the token) must navigate through multiple doors before proceeding. This disparity is palpable when implementing a streaming chat endpoint. In simple terms, Fastify’s compact design minimizes server-side waiting, enabling users to perceive tokens almost as soon as Claude produces them.\n\nThe code snippet below illustrates a minimal Fastify server setup:\n\n```javascript\nimport Fastify from 'fastify';\n\n// Create a Fastify instance with the smallest possible options\nconst fastify = Fastify({ logger: true });\n\nfastify.get('/', async (request, reply) => {\n// A simple health-check endpoint\nreturn { status: 'ok' };\n});\n\nexport default fastify;\n```\n\nFastify initializes the server. The `logger: true` option logs request details, which proves invaluable during development. No extra middleware is loaded, thereby maintaining negligible overhead per request. Configuring Fastify with Node.js 22’s inherent `fetch` function, which mirrors browser behavior, allows seamless communication with AWS Bedrock, the service hosting Claude. Utilizing native `fetch` reduces bundle size, crucial for Lambda, where every byte impacts cold-start time. Avoiding extra dependencies is vital because each additional library must be zipped, uploaded, and unpacked during Lambda initialization. Native `fetch` circumvents this overhead, thereby accelerating cold starts.\n\nThe following code snippet configures a Fastify route to invoke Claude via Bedrock and stream SSE data:\n\n```javascript\nimport { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';\n\nexport async function chatRoute(fastify: FastifyInstance) {\n// Register a POST /chat endpoint\nfastify.post('/chat', async (request: FastifyRequest, reply: FastifyReply) => {\n// Parse the incoming JSON body\nconst { prompt } = request.body as { prompt: string };\n\nif (!prompt) {\nreply.status(400).send({ error: 'Prompt is required' });\nreturn;\n}\n\n// Call the helper function to interact with Bedrock\nconst stream = await invokeClaudeStream(prompt);\n\n// Set up Server-Sent Events (SSE) headers\nreply.raw.writeHead(200, {\n'Content-Type': 'text/event-stream',\n'Cache-Control': 'no-cache',\nConnection: 'keep-alive',\n});\n\n// Stream each token from Bedrock to the client\nfor await (const token of stream) {\nreply.raw.write(`data: ${JSON.stringify({ token })}\\n\\n`);\n}\n\n// Close the SSE stream when Bedrock completes\nreply.raw.end();\n});\n}\n```\n\nThis setup reads the JSON payload the browser transmits, dispatches the prompt to Bedrock, and streams each token as an SSE line. The `reply.raw` variable provides access to the underlying Node `http.ServerResponse`, enabling direct manipulation of raw SSE data. It is advisable to encapsulate fetch calls and response handling within the route to minimize code execution during each request, thus enhancing cold-start performance.",
  "summary": "LLM developers assume they need a heavyweight framework to handle streaming responses, but Fastify’s tiny core can proxy Claude’s token stream with almost zero overhead. In this post you’ll see how to turn a Fastify service into a real‑time chat endpoint that streams tokens directly to the browser. Why Fastify Beats Express for LLM Streaming When you ask a language model (LLM) like Claude to…",
  "key_points": [],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}