Skip to content

Examples: context compression

Shrink what each model call sends. Back to the Examples gallery. Files here: 12.

File Shows Install Run Docs
examples/compression_agent_framework.py Compress what Microsoft Agent Framework sends on every model call, with chat middleware. pip install "agent-adapter[agent-framework]" import only Compression by framework
examples/compression_backend.py No agent framework: a script and a FastAPI route call your model with compression on. pip install agent-adapter; optional: pip install fastapi python examples/compression_backend.py Compress a long context
examples/compression_camel.py Compress the CAMEL ChatAgent memory records it builds each model call's context from. pip install "agent-adapter[camel]" import only Compression by framework
examples/compression_crewai.py Compress a CrewAI agent's messages before each LLM call; they are OpenAI chat dicts. pip install agent-adapter import only Compression by framework
examples/compression_dspy.py Compress the chat messages DSPy builds for each LM call, with an adapter subclass. pip install "agent-adapter[dspy]" import only Compression by framework
examples/compression_framework_hooks.py Compress what Pydantic AI, Agno, Strands, smolagents, AutoGen and AG2 send to the model. pip install agent-adapter; optional: pip install "agent-adapter[ag2,agno,autogen,pydantic-ai,smolagents,strands]" python examples/compression_framework_hooks.py Compression by framework
examples/compression_frameworks.py Compress a framework's own message list where it calls the model; no framework installed. pip install agent-adapter python examples/compression_frameworks.py Compression by framework
examples/compression_langchain.py LangChain alone: compress the message list before model.invoke; no LangGraph needed. pip install "agent-adapter[langchain]" python examples/compression_langchain.py Compression by framework
examples/compression_langgraph.py LangGraph: compress state["messages"] in your agent node, before the model. pip install "agent-adapter[langgraph]" python examples/compression_langgraph.py Compression by framework
examples/compression_llamaindex.py Compress a LlamaIndex ChatMessage list before the LLM call; the list keeps its type. pip install "agent-adapter[llamaindex]" import only Compression by framework
examples/compression_one_call.py One model call, four policies: trim only, cut only, summarise only, and all combined. pip install agent-adapter python examples/compression_one_call.py Compress a long context
examples/compression_semantic_kernel.py Compress a Semantic Kernel ChatHistory copy before the chat service call. pip install "agent-adapter[semantic-kernel]" import only Compression by framework