Demonstrates Autonomous Context Compression in OrkaJS - letting agents decide when to compress their conversation history instead of relying on fixed token thresholds.
- Autonomous Compression: Agent decides when to compress context at clean task boundaries
- Real-time Visualization: See compression events, token savings, and summaries live
- Multi-turn Conversations: Test with long conversations that would normally exceed context limits
- Metrics Dashboard: Track messages compressed, tokens saved, and compression history
┌─────────────────────────────────────────────────────────────────┐
│ Frontend (Next.js) │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────────┐ │
│ │ Chat UI │ │ Metrics │ │ Compression History │ │
│ │ │ │ Dashboard │ │ Timeline │ │
│ └──────┬──────┘ └──────┬──────┘ └───────────┬─────────────┘ │
│ │ │ │ │
│ └────────────────┴──────────────────────┘ │
│ │ │
└──────────────────────────┼───────────────────────────────────────┘
│ HTTP/WebSocket
┌──────────────────────────┼───────────────────────────────────────┐
│ ▼ │
│ Backend (Express) │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ ReActAgent │ │
│ │ ┌─────────────┐ ┌─────────────────────────────────────┐ │ │
│ │ │ Tools │ │ SummaryMemory │ │ │
│ │ │ - search │ │ ┌─────────────────────────────┐ │ │ │
│ │ │ - calc │ │ │ compress() method │ │ │ │
│ │ │ - compact │◄─┼──│ (autonomous compression) │ │ │ │
│ │ └─────────────┘ │ └─────────────────────────────┘ │ │ │
│ │ └─────────────────────────────────────┘ │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ OpenAI API │
└──────────────────────────────────────────────────────────────────┘
# Backend
cd backend && npm install
# Frontend
cd ../frontend && npm install# Backend
cp backend/.env.example backend/.env
# Add your OPENAI_API_KEY# Terminal 1: Backend
cd backend && npm run dev
# Terminal 2: Frontend
cd frontend && npm run devOpen http://localhost:3010 to see the demo.
import { ReActAgent, createCompactConversationTool } from '@orka-js/agent';
import { SummaryMemory } from '@orka-js/memory-store';
import { OpenAIAdapter } from '@orka-js/openai';
const llm = new OpenAIAdapter({ apiKey: process.env.OPENAI_API_KEY });
const memory = new SummaryMemory({ llm, maxMessages: 100 });
// Create the autonomous compression tool
const compactTool = createCompactConversationTool({
compress: async () => {
const result = await memory.compress();
return {
success: result.success,
reason: result.reason,
summary: result.summary,
messagesCompressed: result.messagesCompressed,
tokensSaved: result.tokensSaved,
compressedAt: result.compressedAt,
};
},
});
// Agent can now autonomously decide when to compress
const agent = new ReActAgent({
llm,
goal: 'Help users with complex tasks while managing context efficiently',
tools: [compactTool, searchTool, calculatorTool],
systemPrompt: `You are a helpful assistant with access to a compact_conversation tool.
Use it proactively when:
- The conversation becomes lengthy
- You're transitioning to a new task
- You notice repetitive context`,
});| Method | Endpoint | Description |
|---|---|---|
| POST | /api/chat |
Send a message and get agent response |
| GET | /api/metrics |
Get compression metrics and history |
| POST | /api/compress |
Manually trigger compression |
| DELETE | /api/clear |
Clear conversation history |
| Variable | Description | Required |
|---|---|---|
OPENAI_API_KEY |
OpenAI API key | Yes |
PORT |
Backend port (default: 4010) | No |
MIT - Part of the OrkaJS Showcase collection.