8 production-ready TypeScript snippets covering the patterns you'll use in every AI project. Copy, adapt, and ship.
Render tokens as they arrive instead of waiting for the full response. Essential for chatbot-style UX.
Same pattern using the Anthropic SDK — streams Claude's response token by token.
Force the model to return typed JSON that matches your schema — no more parsing errors.
Retrieval-Augmented Generation: load documents, embed them, then answer questions from your own data.
Let the model decide when to call your functions — the foundation of AI agents.
A complete Next.js App Router API route that streams an LLM response to the browser.
Structure prompts properly with a system role to control model behaviour and output format.
Maintain multi-turn conversation context without a database — great for prototypes.
Before running: Install dependencies with npm install openai @anthropic-ai/sdk langchain @langchain/openai zod and set your API keys in a .env file.