Reference library

AI & LLM integration patterns

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

4 matches
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 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 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.

generator llm streaming
Python
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…
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

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.