Skip to main content
Resources5 min readUpdated July 2026

Code Examples & Recipes

Copy-pasteable production recipes for real-world AI applications.

Recipe: Multi-Turn Chat History Truncation

Compress conversational transcripts while preserving system role context:
chat_recipe.py
from llmslim import compress_chat_messages

conversation_history = [
    {"role": "system", "content": "You are a senior tech lead."},
    {"role": "user", "content": "Can you explain distributed consensus?"},
    {"role": "assistant", "content": "Distributed consensus is... [long 800 word explanation]"},
    {"role": "user", "content": "How does Raft handle leader elections specifically?"}
]

slim_history = compress_chat_messages(conversation_history, target_ratio=0.5)
print(slim_history)

Frequently Asked Questions

Where can I find complete runnable scripts?

Check out the official GitHub repository under the examples/ directory.