Reference library

AI & LLM integration patterns

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

34 matches
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 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 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 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.