How Maka Handles Context Pruning and Compaction in the Runtime Event Log

Maka maintains an immutable Runtime Event Log where context pruning selectively removes events from the provider input projection to fit token limits, while compaction merges historical turns into summarized representations—both operations occur without mutating the underlying log entries, ensuring full replayability and debugging capability.

The Apache Maka framework records every semantic fact of an agent execution—model messages, tool calls, results, and termination events—in an immutable Runtime Event Log. This log serves as the single source of truth for session state, UI views, and recovery mechanisms. When token limits constrain the provider context window, Maka employs sophisticated context pruning and compaction in the Runtime Event Log to optimize what the model sees while preserving the complete execution history for analysis and replay.

The Immutable Runtime Event Log Architecture

According to the project architecture documentation, the Runtime Event Log is the canonical source for model messages, tool calls, tool results, and termination facts. Context pruning and compaction change provider input projections, not the history itself.

As documented in [runtime-core-architecture-draft.md](https://github.com/apache/maka/blob/main/runtime-core-architecture-draft.md#L419), the RuntimeKernel owns the active execution and ensures all components read from the same immutable log. Each component may apply its own projection before sending data to the LLM provider, but the underlying events in packages/runtime/src/runtime-kernel.ts remain untouched.

How Context Pruning Works

Context pruning evaluates the token budget and removes the oldest or least-relevant events from the provider input projection.

Token Budget Evaluation

The evaluation logic resides in [packages/runtime/src/context-budget.ts](https://github.com/apache/maka/blob/main/packages/runtime/src/context-budget.ts). This module computes which events exceed the configured token budget and marks them for exclusion from the next provider request.

Provider Projection Filtering

When the budget is exhausted, the AiSdkBackend in [packages/runtime/src/ai-sdk-backend.ts](https://github.com/apache/maka/blob/main/packages/runtime/src/ai-sdk-backend.ts) checks midTurnState.exhaustedDetail and aborts the current turn with a diagnostic error if necessary (lines 2198–2199). This triggers the pruning logic that filters the event array before serialization to the provider.

Handling Large Tool Results During Pruning

When specific tool results exceed size thresholds, Maka archives them rather than deleting them entirely.

The Tool Result Archive Capability

The pruneStaleToolResultsBeforeCompact function in [packages/runtime/src/tool-result-archive-capability.ts](https://github.com/apache/maka/blob/main/packages/runtime/src/tool-result-archive-capability.ts) (lines 154–240) determines whether a tool result exceeds the budget. If so, it archives the result and returns a lightweight placeholder containing metadata and a reference ID.

Placeholder Replacement Strategy

The StaleToolResultPrunePolicy (lines 165–176) defines the limits, including maxResultEstimatedTokens and minRecentTurnsFull. When enabled, this policy ensures that recent turns remain fully available while older, large results move to archive storage.

History Compaction Without Data Loss

Compaction reduces token count by merging consecutive events into compact representations.

Compaction Trigger Points

The AiSdkBackend coordinates history replay and triggers compaction via the compactHistory mechanism (lines 2655–2656). This operation rewrites the projection—often by summarizing earlier turns—while the original log entries stay intact for replay and debugging.

Summarization Implementation

During compaction, the system may invoke a configurable summarizer to condense multiple turns into a single context entry, preserving essential semantic information without retaining full verbatim history in the active context window.

Configuring Context Budget and Compaction

Developers enable and tune these features through the RuntimeConfig interface passed to the RuntimeKernel.

Enabling Context Budget Pruning

import { RuntimeConfig } from '@maka/runtime';

const config: RuntimeConfig = {
  contextBudget: {
    enabled: true,
    maxTokens: 2048,
    prunePolicy: {
      enabled: true,
      maxResultEstimatedTokens: 1024,
      minRecentTurnsFull: 2,
    },
  },
};

await runtimeKernel.start(config);

Archiving Pruned Tool Results

import { pruneStaleToolResultsBeforeCompact } from '@maka/runtime/tool-result-archive-capability';

const events = /* RuntimeEvent[] */;
const policy = { 
  enabled: true, 
  maxResultEstimatedTokens: 800, 
  minRecentTurnsFull: 1 
};

const { events: prunedEvents, prunedToolResults } =
  pruneStaleToolResultsBeforeCompact(events, policy);

Replaying with Archived Results

import { readArchivedToolResult } from '@maka/runtime/tool-result-archive-capability';

if (turn.hasPlaceholder) {
  const fullResult = await readArchivedToolResult(turn.placeholderId);
  // Process fullResult for debugging or downstream logic
}

Summary

Frequently Asked Questions

Does pruning delete events from the Runtime Event Log?

No. Pruning removes events only from the projection sent to the LLM provider. The immutable Runtime Event Log retains all original entries for debugging, replay, and checkpoint reconstruction.

What happens to tool results that exceed the token budget?

The pruneStaleToolResultsBeforeCompact function archives the full result content and replaces it with a lightweight placeholder in the active projection. The placeholder contains a reference ID that allows later hydration via readArchivedToolResult.

How does compaction differ from pruning?

Pruning selectively excludes specific events from the provider context to fit immediate token limits. Compaction actively merges multiple historical events into summarized representations, reducing token count while attempting to preserve semantic meaning, without modifying the underlying log storage.

Where is the context budget configured in Maka?

The context budget is configured through the RuntimeConfig interface when initializing the RuntimeKernel. The contextBudget object includes maxTokens for the provider window and prunePolicy settings for managing large tool results and stale entries.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →