AIR

Example: context optimization

python
import asyncio
 
from air import AIR, ChatRequest, ChatResponse, Message, Role
from air.config import AIRConfig
from air.context.optimize import ContextBudget
from air.providers.base import Provider
 
 
class EchoProvider(Provider):
    name = "echo"
 
    async def chat(self, request: ChatRequest) -> ChatResponse:
        return ChatResponse(content="ok", provider=self.name, model="demo")
 
 
async def main():
    air = AIR(config=AIRConfig(context_budget=ContextBudget(max_tokens=300, keep_recent=4)))
    air.add_provider(EchoProvider())
 
    # Build a long conversation.
    messages = [
        Message(role=Role.USER if i % 2 == 0 else Role.ASSISTANT, content=f"turn {i} " + "lorem ipsum " * 20)
        for i in range(20)
    ]
    response = await air.chat(ChatRequest(messages=messages))
 
    print("original estimated tokens:", response.original_estimated_tokens)
    print("optimized estimated tokens:", response.optimized_estimated_tokens)
    print("estimated tokens saved:", response.estimated_tokens_saved)
    print("compression applied:", response.compression_applied)
 
 
if __name__ == "__main__":
    asyncio.run(main())

With a small max_tokens budget and a long conversation, compression_applied is True and optimized_estimated_tokens is well below original_estimated_tokens. The most recent 4 messages (keep_recent=4) are still sent to the provider unchanged; only the older ones are replaced by a summary. See ../features/context-optimization.md.