Reference library

AI & LLM integration patterns

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

29 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 JSON Output Against a Dict Schema in Python

Validate JSON-like data against a simple dict schema with type checking and descriptive error messages using only the Python standard library.

json validation schema
Python
from typing import Dict, Any, List, Union

def validate_json(data: Any, schema: Dict[str, str]) -> List[str]:
    """
    Validate JSON-like data against a simple dict schema.
    Schema format: {field_name: expected_type} where type is one of:
    'str', 'int', 'float', 'bool', 'list', 'dict', 'any'
    Returns list …
13 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 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

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.