Code helpers

Battle-tested snippets, ready to paste.

Small, focused pieces of code we reach for again and again. Each one comes with notes on how it works, the gotchas, and when not to use it.

  • Copy, paste, ship
  • Production-minded defaults
  • Explained, not just dumped

16 snippets

TypeScript

Stream LLM responses from a Next.js route handler

Send tokens to the browser as the model produces them instead of making users wait for the full completion. Uses a plain ReadableStream, so it works with any client that can read a fetch body.

AI & LLMs#streaming#nextjs#openai
TypeScript

Retry LLM calls with exponential backoff and jitter

LLM APIs return 429s and 5xx errors under load. This wrapper retries only the failures worth retrying, honours Retry-After and spreads retries out so many workers don't hit the API in lockstep.

AI & LLMs#retry#backoff#resilience
Python

Text chunker for RAG with overlap

Split long documents into overlapping chunks that end on paragraph or sentence boundaries, so retrieval doesn't return half-sentences. No dependencies beyond the standard library.

AI & LLMs#rag#embeddings#chunking
TypeScript

Redis sliding-window rate limiter

Limit requests per user, API key or IP across every instance of your service. A single Lua script keeps the check-and-increment atomic, so concurrent requests can't slip past the limit.

Backend#redis#rate-limit#ioredis
TypeScript

Verify webhook HMAC signatures

Confirm that an incoming webhook was sent by the provider and wasn't replayed. Uses a constant-time comparison and a timestamp tolerance, the same scheme Stripe and many others use.

Backend#webhooks#security#hmac
TypeScript

Graceful shutdown for Node.js HTTP servers

Finish in-flight requests and close database connections before the process exits, so deploys and autoscaling don't drop requests or leave half-written work.

Backend#nodejs#kubernetes#reliability
TypeScript

useDebounce hooks for React

Wait until the user stops typing before firing a search or autosave. Includes a value version for derived state and a callback version for event handlers.

Frontend#react#hooks#debounce
TypeScript

SSR-safe useLocalStorage hook

Persist small bits of UI state in localStorage without hydration mismatches in Next.js. Built on useSyncExternalStore, so it also stays in sync across components and browser tabs.

Frontend#react#hooks#nextjs
TypeScript

fetch with timeout, cancellation and typed errors

fetch has no default timeout and doesn't throw on 4xx/5xx. This small wrapper adds both, and still lets callers cancel with their own AbortSignal.

Frontend#fetch#abortcontroller#timeout
Dockerfile

Multi-stage Dockerfile for Next.js standalone output

Build a small production image for a Next.js app that runs as a non-root user and contains only the traced server files, not the full node_modules.

DevOps#docker#nextjs#containers
YAML

GitHub Actions CI: lint, test, build and push to GHCR

Run checks on every pull request, and on main build a Docker image and push it to GitHub Container Registry with layer caching. No long-lived registry credentials needed.

DevOps#github-actions#ci#docker
YAML

Kubernetes Deployment with probes and zero-downtime rollouts

A production-ready baseline for a stateless HTTP service: readiness, liveness and startup probes, resource limits, a non-root security context and rolling updates that never drop capacity.

DevOps#kubernetes#deployment#probes
Python

Sort incoming documents into folders with an LLM

A small worker that watches an inbox folder, classifies each document with an LLM and moves it to the right place. Low-confidence results go to a review folder instead of being guessed.

Automation#python#openai#classification
TypeScript

Forward webhooks to Slack

Turn events from any tool that can send a webhook (monitoring, payments, forms, CI) into readable Slack messages, protected by a shared secret.

Automation#slack#webhooks#notifications
SQL

Bulk upsert in Postgres with ON CONFLICT

Insert or update many rows in one round trip, skip writes when nothing changed, and find out which rows were new. Useful for syncing data from CRMs, APIs or CSV imports.

Data#postgres#sql#upsert
TypeScript

Keyset (cursor) pagination for Postgres

Paginate large tables with stable, fast queries that don't slow down on deep pages and don't skip or repeat rows when new data arrives, unlike OFFSET.

Data#postgres#pagination#sql

Start a project

Have a project in mind? Let's build it.

Tell us what you're working on. We'll reply within one business day with ideas, a plan and next steps.