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

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…
14 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 medium

Circuit Breaker Pattern in Python for LLM API Calls

Implements a circuit breaker class that wraps LLM client calls to fail fast when the service is degrading, then recover automatically after a timeout.

circuit-breaker llm resilience
Python
import time

class CircuitBreaker:
    def __init__(self, failure_threshold=3, recovery_timeout=5):
        self.failure_threshold = failure_threshold
        self.recovery_timeout = recovery_timeout
        self.failure_count = 0
        self.state = "closed"
        self.last_failure_time = None

    def call(self, …
14 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 Accumulate Streamed Tokens into a Final String in Python

Accumulate a stream of tokens into a single final string by concatenating each token in sequence.

streaming tokens strings
Python
def accumulate_tokens(tokens):
    """Accumulate a stream of tokens into a single final string."""
    result = ""
    for token in tokens:
        result += token
    return result


if __name__ == "__main__":
    token_stream = ["Hello", ", ", "world", "!", " This ", "is ", "accumulated."]
    final_string = accumul…
16 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 medium

How to Build a Data Helper for LLM Prompts in Python

A beginner-friendly helper class that flattens nested dictionaries, formats prompt templates, and safely parses JSON for AI/LLM pipelines.

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


class DataHelper:
    """Simple helper class for working with data in AI/LLM pipelines."""
    
    def __init__(self, data: Optional[Dict[str, Any]] = None) -> None:
        self.data = data or {}
    
    def flatten(self, prefix: str = "") -> Dict[str, Any]…
17 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…
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 Compute a Mock BLEU Score with n-gram Overlap in Python

Evaluate text similarity with a simplified BLEU score using word-level n-gram precision and a brevity penalty.

bleu n-grams text evaluation
Python
from collections import Counter

def bleu_score(reference, candidate, n=2):
    """
    Compute a simplified BLEU score with n-gram precision and brevity penalty.
    Mock demo using word-level n-grams.
    """
    ref_tokens = reference.lower().split()
    cand_tokens = candidate.lower().split()
    
    # Compute n-…
12 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 Mock Text Embedding with Hash in Python

Generate deterministic mock text embeddings using SHA-256 hashing and numpy, producing normalized vectors for similarity testing without an LLM.

embeddings hashing numpy
Python
import hashlib
import numpy as np

def mock_embed(text: str, dim: int = 10, seed: int = 42) -> np.ndarray:
    """Generate a deterministic mock embedding using a hash function.
    
    Args:
        text: Input text to embed
        dim: Dimension of the output vector
        seed: Seed for reproducibility
    
    R…
13 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 medium

How to Detect Prompt Injection in Python

Implements a regex-based heuristic in Python to flag common prompt injection attempts before sending input to an LLM.

prompt-injection regex llm-security
Python
import re

def contains_prompt_injection(user_input: str) -> bool:
    # Directives to ignore previous instructions or act as system
    ignore_patterns = [
        r"\bignore\s+(all\s+)?previous\s+instructions\b",
        r"\bdisregard\s+(all\s+)?previous\s+instructions\b",
        r"\bdon'?t\s+follow\s+(any\s+)?inst…
13 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 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 Keep Last K Turns in a Memory Buffer in Python

A TurnBuffer class using deque with maxlen to keep only the most recent k conversation turns in memory for LLM context.

deque llm-context memory-buffer
Python
from collections import deque

class TurnBuffer:
    def __init__(self, k):
        self.k = k
        self.turns = deque(maxlen=k)

    def add(self, turn):
        self.turns.append(turn)

    def last_k(self):
        return list(self.turns)


if __name__ == "__main__":
    buffer = TurnBuffer(3)
    buffer.add("tu…
14 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 an LLM Client in Python

Create a simple mock LLM client that returns a canned completion for testing or development without a real API.

llm mock testing
Python
from dataclasses import dataclass


@dataclass
class MockLLMClient:
    canned_response: str = "This is a canned completion."

    def complete(self, prompt: str) -> str:
        return f"{self.canned_response} [to: {prompt[:20]}]"


if __name__ == "__main__":
    client = MockLLMClient()
    result = client.complete(…
16 0 Open
AI & LLM integration patterns easy

How to Parse JSON from LLM Model Output Fence in Python

Extract and parse a JSON object from a language model's output that may be wrapped in triple-backtick fences with an optional language tag.

json llm parsing
Python
import json
import re

def parse_json_from_fence(text):
    """
    Extract JSON object from a model output that may be wrapped in
    triple-backtick fences with optional language tag.
    """
    # Match content inside
12 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

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.