Reference library

AI & LLM integration patterns

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

15 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 Append Few-Shot Examples to a Prompt in Python

This code builds a complete LLM prompt by appending few-shot examples in alternating user/assistant format using a simple loop.

prompt-engineering few-shot llm
Python
def append_few_shot_examples(prompt: str, examples: list[tuple[str, str]], separator: str = "\n\n") -> str:
    """Append few-shot examples to a prompt in alternating user/assistant format."""
    full_prompt = prompt
    for user_input, assistant_output in examples:
        full_prompt = f"{full_prompt}{separator}Use…
15 0 Open
AI & LLM integration patterns easy

How to Build a Prompt Template with Variable Slots in Python

Create a reusable LLM prompt template with named variable slots using Python's string.Template class and fill them with render() calls.

llm prompt-engineering templates
Python
from string import Template


class PromptTemplate:
    def __init__(self, template_text):
        self.template = Template(template_text)

    def render(self, **kwargs):
        return self.template.substitute(**kwargs)


if __name__ == "__main__":
    template = PromptTemplate(
        "You are a helpful assistant …
14 0 Open
AI & LLM integration patterns easy

How to Build a Simple Semantic Cache for Similar Prompts in Python

Mock a semantic cache that finds the closest matching prompt using word-overlap similarity and returns cached results above a threshold.

semantic cache prompt matching llm
Python
prompt_cache = [
    "What is the capital of France?",
    "How does recursion work?",
    "Best practices for Python logging?",
    "Explain binary search in one line.",
    "How to reverse a string in Python?"
]

def normalize(text):
    return " ".join(text.lower().split())

def similarity(a, b):
    a_words = set(…
14 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…
13 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 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 Render a Jinja-like Template from a Dict in Python

Replace {{placeholders}} in a string using values from a Python dict with a simple regex-based template renderer.

templating regex strings
Python
import re

def render_template(template, context):
    pattern = re.compile(r"\{\{\s*(\w+)\s*\}\}")
    def replace(match):
        key = match.group(1)
        return str(context.get(key, ""))
    return pattern.sub(replace, template)

if __name__ == "__main__":
    template = "Hello {{name}}, you have {{count}} new …
14 0 Open
AI & LLM integration patterns easy

How to hash a prompt with SHA-256 in Python

Create a SHA-256 hex fingerprint of a prompt string, with a short-prefix variant for quick references.

hashlib sha256 fingerprint
Python
import hashlib

def prompt_hash_fingerprint(prompt: str) -> str:
    """Return the full SHA-256 hex digest of the prompt."""
    return hashlib.sha256(prompt.encode("utf-8")).hexdigest()

def short_fingerprint(prompt: str, length: int = 12) -> str:
    """Return a short prefix of the SHA-256 digest for quick reference…
13 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

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.