AI & LLM integration patterns
Call LLM APIs, structure prompts, parse responses, and ship AI features safely.
How to Chunk a Long Document for RAG Retrieval in Python
Split text into overlapping chunks at sentence boundaries using a custom Python function suitable for RAG retrieval pipelines.
import re
from pathlib import Path
def chunk_document(text, chunk_size=500, overlap=100):
"""Split text into overlapping chunks suitable for RAG retrieval."""
# Normalize whitespace
text = re.sub(r'\s+', ' ', text).strip()
chunks = []
start = 0
while start < len(text):
end = min(s…
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.
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("
How to Stream Tokens from a Mock LLM in Python
Simulate real-time LLM streaming by yielding tokens one at a time with a delay, making it easy to test streaming UIs.
import time
from typing import Generator
def stream_tokens(text: str, delay: float = 0.05) -> Generator[str, None, None]:
"""Simulate an LLM streaming tokens word by word."""
for word in text.split():
yield word
time.sleep(delay)
if __name__ == "__main__":
sample = "Hello world! This is…
How to Summarize Old Conversation Turns in Python
Compress old conversation turns into a brief summary while keeping recent turns intact for LLM context management.
from datetime import datetime, timedelta
def summarize_old_turns(conversation, max_turns=5):
"""Compress turns older than max_turns into a brief summary."""
if len(conversation) <= max_turns:
return conversation, ""
old_turns = conversation[:-max_turns]
recent_turns = conversation[-max_turns…
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.
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…
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.