Reference library

AI & LLM integration patterns

Call LLM APIs, structure prompts, parse responses, and ship AI features safely.

43 matches
AI & LLM integration patterns easy

How to Redact Emails and Phones Before Sending to an LLM in Python

This code uses regular expressions to replace email addresses and US phone numbers with [EMAIL] and [PHONE] placeholders before any LLM processing.

pii redaction regular-expressions
Python
import re

def redact_pii(text: str) -> str:
    # Replace email addresses with [EMAIL]
    text = re.sub(r'[\w.+-]+@[\w-]+\.[\w.-]+', '[EMAIL]', text)
    # Replace phone numbers (US format) with [PHONE]
    text = re.sub(r'\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}', '[PHONE]', text)
    return text

if __name__ == "__main…
15 0 Open
AI & LLM integration patterns medium

How to Repair Malformed JSON Braces Heuristically in Python

Heuristically fix malformed JSON by balancing braces and quotes, using a stack-based approach to add missing closing characters.

json repair heuristic
Python
import json
import re

def repair_json(text: str) -> str:
    """Heuristically repair malformed JSON by balancing braces and quotes."""
    # Trim whitespace and handle leading/trailing garbage
    text = text.strip()
    
    # Remove common non-JSON decorations
    text = re.sub(r'^(
13 0 Open
AI & LLM integration patterns medium

How to Retry LLM Calls on Rate Limit Errors in Python

Implement a retry mechanism with exponential backoff for LLM API calls that raises a custom RateLimitError, using a mock function to demonstrate the pattern.

llm retry rate-limit
Python
import time
import random


def mock_llm_call():
    """Simulates an LLM API call that may raise a rate limit error."""
    if random.random() < 0.4:  # 40% chance of rate limit
        raise RateLimitError("Rate limit exceeded. Try again later.")
    return {"response": "Hello world from mock LLM"}


class RateLimitE…
16 0 Open
AI & LLM integration patterns easy

How to Stream Tokens from a Mock LLM in Python

Simulate real-time LLM streaming by yielding tokens one at a time with a delay, making it easy to test streaming UIs.

generator llm streaming
Python
import time
from typing import Generator


def stream_tokens(text: str, delay: float = 0.05) -> Generator[str, None, None]:
    """Simulate an LLM streaming tokens word by word."""
    for word in text.split():
        yield word
        time.sleep(delay)


if __name__ == "__main__":
    sample = "Hello world! This is…
15 0 Open
AI & LLM integration patterns easy

How to Summarize Old Conversation Turns in Python

Compress old conversation turns into a brief summary while keeping recent turns intact for LLM context management.

llm context compression
Python
from datetime import datetime, timedelta


def summarize_old_turns(conversation, max_turns=5):
    """Compress turns older than max_turns into a brief summary."""
    if len(conversation) <= max_turns:
        return conversation, ""

    old_turns = conversation[:-max_turns]
    recent_turns = conversation[-max_turns…
13 0 Open
AI & LLM integration patterns easy

How to Truncate Text to a Token Budget in Python

Truncate a string to a maximum token budget for LLM context using the tiktoken library and OpenAI's tokenizer.

tiktoken llm tokens
Python
import tiktoken

def truncate_to_token_budget(text, max_tokens, model="gpt-3.5-turbo"):
    enc = tiktoken.encoding_for_model(model)
    tokens = enc.encode(text)
    if len(tokens) <= max_tokens:
        return text
    truncated_tokens = tokens[:max_tokens]
    return enc.decode(truncated_tokens)

if __name__ == "__…
16 0 Open
AI & LLM integration patterns easy

How to Validate LLM Output in Python

A beginner-friendly DataValidator class that checks required fields and type constraints on LLM-generated or user JSON data.

validation llm json
Python
import json
from typing import Any, Dict, List, Optional


class DataValidator:
    """Simple helper for validating LLM-generated or user data."""

    def __init__(self, required_fields: List[str], schema: Optional[Dict[str, str]] = None):
        self.required_fields = required_fields
        self.schema = schema or…
14 0 Open
AI & LLM integration patterns easy

How to build a function calling schema dict in Python

Build an OpenAI-compatible function calling schema dictionary with a helper function that takes name, description, parameters, and required fields.

llm-api function-calling schema
Python
import json
from typing import Dict, Any, List, Optional


def build_function_schema(
    name: str,
    description: str,
    parameters: Optional[Dict[str, Any]] = None,
    required: Optional[List[str]] = None
) -> Dict[str, Any]:
    """Build an OpenAI-compatible function calling schema dictionary."""
    schema: …
14 0 Open
AI & LLM integration patterns easy

How to build a mock RAG pipeline in Python

Build a minimal Retrieval-Augmented Generation pipeline that retrieves the best-matching document by keyword overlap and generates a template-based answer.

rag llm retrieval
Python
def simple_rag_pipeline(question, documents):
    """
    A minimal mock RAG pipeline: retrieve relevant context, then generate an answer.
    """
    # Step 1: Retrieve — mock retrieval by simple keyword scoring
    scores = []
    for doc in documents:
        doc_words = set(doc.lower().split())
        question_wo…
14 0 Open
AI & LLM integration patterns medium

How to cache embeddings with a Python dict to avoid recomputation

Caches embeddings computed from text in a dictionary keyed by SHA-256 hash, returning cached results for repeated calls.

embedding cache dict
Python
import hashlib
import time


class EmbeddingCache:
    def __init__(self):
        self.cache = {}

    def _hash_text(self, text):
        return hashlib.sha256(text.encode()).hexdigest()

    def get_embedding(self, text, compute_func):
        key = self._hash_text(text)
        if key not in self.cache:
          …
15 0 Open
AI & LLM integration patterns easy

How to compute exact match metric in Python

Computes the exact match (EM) metric for LLM outputs by normalizing text and comparing predictions against references.

exact-match metric evaluation
Python
def compute_exact_match(predictions, references):
    def normalize(text):
        import re
        text = text.lower().strip()
        text = re.sub(r'\b(a|an|the)\b', ' ', text)
        text = re.sub(r'[^a-z0-9\s]', '', text)
        text = ' '.join(text.split())
        return text

    matches = sum(1 for pred, r…
12 0 Open
AI & LLM integration patterns medium

How to implement exponential backoff for LLM API calls in Python

A decorator that retries flaky LLM API calls with exponential delay, using a mock client to demonstrate the pattern.

exponential-backoff retries llm
Python
import time
import random

class MockLLM:
    def call(self, prompt):
        if random.random() < 0.7:  # 70% chance of transient failure
            raise ConnectionError("API unavailable")
        return f"LLM response for: {prompt}"

def with_exponential_backoff(max_retries=5, base_delay=0.1):
    def decorator(fu…
14 0 Open
AI & LLM integration patterns medium

How to parallel map embeddings with a thread pool in Python

Run embedding computations in parallel using ThreadPoolExecutor, collect results into a dict keyed by the original item.

concurrency threadpool embeddings
Python
import threading
from concurrent.futures import ThreadPoolExecutor
import time


def compute_embedding(item: int) -> tuple[int, int]:
    time.sleep(0.05)  # Simulate embedding work
    return item, item * 10


def parallel_map_embed(items, max_workers=3):
    results = {}
    with ThreadPoolExecutor(max_workers=max_w…
15 0 Open
AI & LLM integration patterns easy

How to randomly assign a prompt variant to each key in Python

Randomly pick one variant from a list for each prompt key, useful for A/B testing message variations.

random dictionary a/b-testing
Python
import random

def assign_prompt_variant(prompts: dict[str, list[str]]) -> dict[str, str]:
    """Assign a random prompt variant to each prompt key."""
    return {key: random.choice(variants) for key, variants in prompts.items()}

if __name__ == "__main__":
    prompt_bank = {
        "greeting": ["Hello!", "Hi there…
14 0 Open
AI & LLM integration patterns easy

JSON Mode Prompt Schema Output in Python

Extract a user object to JSON with explicit schema keys, ready for LLM JSON-mode prompts.

json schema llm
Python
import json
from typing import Any, Dict


def extract_user_as_json(user: Dict[str, Any]) -> str:
    """Extract a user object and return it as JSON using explicit schema keys."""
    schema_fields = ("id", "name", "email", "is_active")
    user_subset = {key: user[key] for key in schema_fields if key in user}
    ret…
13 0 Open
AI & LLM integration patterns medium

Parse ReAct Logs into Thought Action Observation Steps in Python

Parse a ReAct agent's textual log into structured steps with thought, action, and observation using regex and named tuples.

react regex llm
Python
import re
from collections import namedtuple


ReActStep = namedtuple("ReActStep", ["thought", "action", "observation"])


def parse_react_log(log: str) -> list[ReActStep]:
    """Parse a ReAct log into structured thought/action/observation steps."""
    pattern = re.compile(
        r"Thought:\s*(?P<thought>.+?)\s*"
…
13 0 Open
AI & LLM integration patterns easy

Prepare LLM prompt data with a Python helper class

A beginner-friendly Python class that collects records, converts them to JSON, and produces a quick summary for building LLM prompt context.

llm json prompt-engineering
Python
import json
from typing import Any, Dict, List

class DataHelper:
    """Simple helper to prepare data for LLM prompts."""
    
    def __init__(self):
        self.data = []
    
    def add(self, item: Dict[str, Any]) -> "DataHelper":
        self.data.append(item)
        return self
    
    def to_json(self) -> s…
16 0 Open
AI & LLM integration patterns easy

Route Tool Call Name to Python Handler Dict

Routes a tool call name to the correct Python handler function using a dictionary lookup, returning an error for unknown tools.

tool-calls llm-integration dictionary-mapping
Python
def get_name():
    return {"name": "Alice"}

def get_age():
    return {"age": 30}

def get_email():
    return {"email": "alice@example.com"}

handlers = {
    "get_name": get_name,
    "get_age": get_age,
    "get_email": get_email,
}

def route(tool_call):
    handler = handlers.get(tool_call["name"])
    if handl…
12 0 Open
AI & LLM integration patterns easy

Serialize and Format Data for LLM Prompts in Python

Use dataclasses and the json module to convert Python objects to JSON strings, parse them back, and format structured data into prompt-friendly text for LLM calls.

dataclasses json llm
Python
import json
from dataclasses import dataclass, asdict


@dataclass
class Recipe:
    """Simple data model to represent a recipe."""
    name: str
    cuisine: str
    prep_minutes: int


def to_json(recipe: Recipe) -> str:
    """Serialize a Recipe to a JSON string."""
    return json.dumps(asdict(recipe), indent=2)

…
14 0 Open

Browse by section

Each section groups closely related Python snippets.

AI & LLM integration patterns — Python code examples

What you will find here

This page collects ai & llm integration patterns snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.