I spent the last few weeks pulling together 10 Python patterns I keep rebuilding on every LLM project, and three mistakes showed up over and over — including in my own early code.
Mixing sync and streaming behind a flag.
It's tempting to write one ask() function with an if stream: branch. Don't. Callers of the blocking version want a plain string they can log, cache, and unit test. Callers of the streaming version want a generator built for a UI. Collapsing both into one function with a boolean flag means every caller has to know which mode they're in — and testing gets messy fast. Two small functions beat one clever one.Retrying every failure the same way.
Wrapping an API call in a retry loop feels like "production hardening" — until you realize you're retrying a malformed request four times with exponential backoff instead of failing fast. The fix is boring but important: only retry transient errors (rate limits, timeouts), and let everything else surface immediately. Pair that with a fallback model (cheap model first, stronger model if it keeps failing), and you've got the pattern most real LLM gateways actually use in production.Re-embedding your entire document library on every restart.
This one's a silent cost killer. In RAG demos, it's common to load PDFs, chunk them, embed them, and query — all in one script, every single run. In production, you build the index once, persist it to disk, and load it on startup. Skipping this step is the single most common reason RAG demos rack up huge embedding bills and never make it past week one.
None of these are exotic. They're just the difference between "code that works when I run it" and "code that doesn't wake me up at 2am."
I ended up writing these patterns down properly — full runnable files, not fragments, covering both the OpenAI and Anthropic SDKs: streaming wrappers, tool/function calling, dynamic system prompts, RAG with LangChain and LlamaIndex, conversational memory with context trimming, vector search from scratch with FAISS, and the retry/fallback wrapper above.
Put them together into The AI & LLM Integration Cookbook — 10 copy-paste-adapt templates, each with a short "why this pattern" note so you're not just copying code, you understand the tradeoff behind it.
If you're tired of rebuilding the same LLM plumbing from scratch on every project, it might save you a weekend. Link in the comments — happy to answer questions about any of the patterns above in the meantime.
Top comments (3)
its 20% off for 1st 5 buyers🤩
check link in bio
Some comments may only be visible to logged-in visitors. Sign in to view all comments.