Generative AI Code Examples: Practical Patterns for Developers
Manas Takalpati
Founder, Blue Orchid
The best way to learn generative AI development is through real code patterns. These are the patterns I use daily in production.
Pattern 1: Basic API Call
The foundation of everything. Send a message, get a response. Works the same across every major API - only the import and model name change.
Key concepts:
- System prompt sets the AI's behavior
- User message is the input
- Temperature controls creativity (0 = deterministic, 1 = creative)
- Max tokens limits response length
Pattern 2: Streaming Responses
Never make users stare at a loading spinner. Stream tokens as they generate.
Why it matters:
- First token appears in ~200ms instead of waiting 2-5 seconds for the full response
- Users perceive the response as faster even though total time is the same
- Essential for any chat-like interface
Implementation approach: Use the streaming API option from your provider, read from the stream chunk by chunk, and render each chunk to the UI immediately.
Pattern 3: Structured Outputs
Get JSON instead of prose. Critical for building reliable AI features.
Use cases:
- Classification (sentiment, category, intent)
- Extraction (names, dates, amounts from text)
- Decision-making (approve/reject with reasoning)
Implementation approach: Define a JSON schema in your prompt or use the provider's structured output feature. Always validate the response against your expected schema.
Pattern 4: Tool Use
Let the AI call functions in your application. This is the bridge from generative to agentic AI.
Common tools:
- Database queries
- API calls to external services
- File operations
- Calculations and data processing
Implementation approach: Define tool schemas with names, descriptions, and parameter types. Send them alongside your messages. Handle tool call responses by executing the function and returning results.
Pattern 5: RAG (Retrieval-Augmented Generation)
Feed relevant context from your data into the AI prompt. The most common pattern for domain-specific AI features.
How it works:
- Convert your documents into vector embeddings
- When a user asks a question, find the most relevant documents
- Include those documents in the prompt as context
- AI answers using your specific data, not just general knowledge
When to use: Customer support bots, documentation search, internal knowledge bases - any time you need AI to answer from your data.
Pattern 6: Prompt Chaining
Break complex tasks into sequential AI calls. Each call builds on the previous output.
Example workflow:
- Call 1: Extract key information from raw text
- Call 2: Classify the extracted information
- Call 3: Generate a response based on the classification
Why chain instead of one big prompt: Each step is simpler and more reliable. Easier to debug. Can use different models for different steps (cheap model for extraction, expensive for generation).
Putting It Together
These patterns combine to build real features:
- AI chatbot: Streaming + RAG + conversation history
- Content generator: Basic call + structured output + prompt chaining
- Data processor: Tool use + structured output + batch processing
- AI agent: Tool use + prompt chaining + ReAct loop
For the complete guide to building with AI, see How to Code AI. For API selection, see Best AI APIs.
Frequently Asked Questions
Want more? Get tutorials and insights straight to your inbox.