Reference library

AI & LLM integration patterns

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

24 matches
AI & LLM integration patterns easy

Cache LLM Completions by Hashing the Prompt in Python

A simple in-memory cache that stores LLM completions keyed by a SHA-256 hash of the prompt to avoid recomputing identical requests.

llm caching hashing
Python
import hashlib
import json

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

    def _hash_prompt(self, prompt: str) -> str:
        return hashlib.sha256(prompt.encode("utf-8")).hexdigest()

    def get(self, prompt: str) -> str | None:
        key = self._hash_prompt(prompt)
        return self.ca…
15 0 Open
AI & LLM integration patterns easy

Chain of Thought Prompting in Python: Step-by-Step Reasoning Demo

This demo shows how to structure a function that explains its own reasoning step-by-step, mimicking chain-of-thought prompting for AI systems.

ai llm reasoning
Python
def solve_math_step_by_step(expression: str) -> str:
    """Solves a simple expression, showing each reasoning step."""
    # Step 1: Parse the expression (assume "a + b" or "a - b")
    parts = expression.split()
    a = int(parts[0])
    op = parts[1]
    b = int(parts[2])
    
    steps = []
    steps.append(f"Step…
16 0 Open
AI & LLM integration patterns easy

Demonstrate Prompt Injection Bypass in Python

Simulate why naive system prompt filters fail against prompt injection with casing and spacing variations.

prompt-injection llm-security demo
Python
# Demonstrate why system prompts can be bypassed by simulated user input
# This demo shows a naive filter being ignored via prompt injection

def process_user_message(message, system_rules):
    """Simulate an AI that follows system rules but gets tricked."""
    # Claim to check system rules
    for rule in system_ru…
14 0 Open
AI & LLM integration patterns easy

How to Batch Embed a List of Strings in Python

Batch embed a list of strings into deterministic pseudo-random vectors using a mock encoder class.

embedding batch-processing mock-encoder
Python
class MockEncoder:
    def __init__(self, dim=8, seed=42):
        self.dim = dim
        self.seed = seed

    def embed(self, text):
        # Deterministic pseudo-random embedding based on text content
        hash_val = hash(text)
        import random
        rng = random.Random(hash_val + self.seed)
        retu…
12 0 Open
AI & LLM integration patterns easy

How to Build a System-User-Assistant Message List in Python

Use dataclasses to model a chat conversation and build the system/user/assistant message list expected by LLM APIs.

llm dataclass openai
Python
from dataclasses import dataclass, field
from typing import List


@dataclass
class Message:
    role: str
    content: str


@dataclass
class Conversation:
    messages: List[Message] = field(default_factory=list)

    def add_system(self, content: str) -> None:
        self.messages.append(Message(role="system", con…
12 0 Open
AI & LLM integration patterns easy

How to Build a Zero-Shot Classification Prompt in Python

Creates a prompt for zero-shot text classification by pairing input text with candidate labels and a hypothesis template.

zero-shot prompt classification
Python
from typing import Dict, List


def build_zero_shot_prompt(
    text: str,
    candidate_labels: List[str],
    hypothesis_template: str = "This is about {}.",
) -> Dict[str, List[str]]:
    """Build a prompt ready for zero-shot classification."""
    return {
        "sequences": text,
        "candidate_labels": can…
13 0 Open
AI & LLM integration patterns easy

How to Build an Agent Loop with Plan, Act, Observe in Python

Implements a simple plan-act-observe loop that an AI agent uses to iteratively complete a task in an environment while storing observations in memory.

agents loop llm
Python
class Agent:
    def __init__(self, name):
        self.name = name
        self.memory = {}

    def plan(self, task):
        return f"Plan for {task}: step 1, step 2, step 3"

    def act(self, plan, environment):
        return f"Executing {plan} in {environment}"

    def observe(self, action_result):
        sel…
17 0 Open
AI & LLM integration patterns easy

How to Build an Entity Memory Dict to Store Facts in Python

Store and recall facts about entities using nested dictionaries with remember, recall, and forget functions in Python.

memory dict nested-dict
Python
facts = {}

def remember(entity, attribute, value):
    if entity not in facts:
        facts[entity] = {}
    facts[entity][attribute] = value

def recall(entity, attribute):
    return facts.get(entity, {}).get(attribute, None)

def forget(entity, attribute=None):
    if attribute is None:
        facts.pop(entity, …
12 0 Open
AI & LLM integration patterns easy

How to Convert Data to JSON and Back in Python

Convert a Python dict into a JSON string with indentation, then parse it back into a dict, demonstrating a common round-trip conversion for beginners.

json serialization conversion
Python
import json
from datetime import datetime

def convert_data(data):
    """Convert a dict into a JSON string and back to dict."""
    json_str = json.dumps(data, indent=2)
    parsed = json.loads(json_str)
    return json_str, parsed

def main():
    sample_data = {
        "user": "alice",
        "message": "hello",
…
11 0 Open
AI & LLM integration patterns easy

How to Create a Mock LLM Judge Rubric Score in Python

Scores a response against a rubric by counting keyword matches, returning total, percentage, and per-criterion feedback.

llm evaluation rubric
Python
def judge_score(response, rubric):
    """Mock LLM judge that scores a response against a rubric."""
    total = 0
    max_total = 0
    feedback = []

    for criterion, rubric_item in rubric.items():
        max_points = rubric_item["max"]
        description = rubric_item["description"]

        # Simple mock scori…
15 0 Open
AI & LLM integration patterns easy

How to Create a Simple Data Helper in Python for LLM Projects

Create a beginner-friendly Python class that stores, filters, and serializes data records for AI/LLM workflows.

data-helper json llm
Python
import json
from typing import Any, Dict, List, Optional


class DataHelper:
    """Simple helper for beginners to manage data in AI/LLM projects."""

    def __init__(self, data: Optional[List[Dict[str, Any]]] = None) -> None:
        self.data: List[Dict[str, Any]] = data or []

    def add_item(self, item: Dict[str…
14 0 Open
AI & LLM integration patterns easy

How to Estimate Token Count in Python

Estimates tokens in a text string using a whitespace and punctuation heuristic without external libraries.

token-count llm heuristic
Python
def estimate_tokens(text: str) -> int:
    """Estimate token count using whitespace and punctuation heuristics."""
    if not text:
        return 0

    words = text.split()
    total_punctuation = sum(1 for char in text if char in ".,!?;:")
    special_tokens = sum(1 for char in text if char in "\n\t")

    # Rough …
12 0 Open
AI & LLM integration patterns easy

How to Filter Blocked Words in Python

Scans input text against a moderation blocklist, returning blocked terms and their counts.

moderation blocklist security
Python
MODERATION_BLOCKLIST = {"spam", "scam", "fraud", "phishing", "malware", "abuse"}

def scan_text(text: str) -> dict:
    normalized = text.lower()
    words = normalized.replace(".", " ").replace(",", " ").replace("!", " ").replace("?", " ").split()
    
    found_terms = []
    for word in words:
        if word in MO…
12 0 Open
AI & LLM integration patterns easy

How to Filter Toxic Keywords in Python

Filter toxic keywords from text by replacing each occurrence with asterisks, useful as a basic guardrail for LLM inputs.

guardrails text-filtering llm-safety
Python
TOXIC_KEYWORDS = ["insult", "threat", "hate", "violence", "spam"]


def guardrails_filter(text: str, keywords: list[str] | None = None) -> str:
    """Filter out toxic keywords from the given text.

    Args:
        text: The input text to filter.
        keywords: Optional keyword list. Defaults to TOXIC_KEYWORDS.

…
12 0 Open
AI & LLM integration patterns easy

How to Log Prompts and Completions as JSONL Audit Files in Python

Read a JSONL file of LLM prompt–completion pairs, compute totals and averages, then write an audit summary with timestamps.

jsonl audit llm
Python
import json
from pathlib import Path
from datetime import datetime


def audit_jsonl(filepath):
    logs = []
    with open(filepath, encoding="utf-8") as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
            entry = json.loads(line)
            logs.ap…
15 0 Open
AI & LLM integration patterns easy

How to Mock OpenAI Tool Call Messages in Python

Create an assistant message with a function tool call in OpenAI's chat format, useful for testing and mocking.

openai tool-calls mock
Python
from openai import OpenAI


def mock_tool_call(tool_name: str, arguments: dict) -> dict:
    """Simulate a tool call message in OpenAI style."""
    return {
        "role": "assistant",
        "content": None,
        "tool_calls": [
            {
                "id": "call_" + "a1b2c3d4e5f6",
                "type…
14 0 Open
AI & LLM integration patterns easy

How to Parse Chat Completion JSON in Python

Parse a mock OpenAI chat completion JSON response into a clean dictionary with content, finish reason, and model.

json openai chat-completion
Python
import json

def parse_chat_response(raw: str) -> dict:
    data = json.loads(raw)
    choice = data["choices"][0]
    return {
        "content": choice["message"]["content"],
        "finish_reason": choice["finish_reason"],
        "model": data["model"],
    }

if __name__ == "__main__":
    mock_response = '''
  …
14 0 Open
AI & LLM integration patterns easy

How to Parse an LLM Response in Python

This code parses a JSON string from an LLM response, stripping code fences and handling common issues like whitespace, returning a Python dictionary.

llm json parsing
Python
import json
from typing import Any, Dict, List


def parse_llm_response(response: str) -> Dict[str, Any]:
    """Parse a JSON string from an LLM response, handling common edge cases."""
    # Remove code fences if present
    cleaned = response.strip()
    if cleaned.startswith("
13 0 Open
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 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.