Context Management

The Context Manager maintains a 3-layer system to give agents the right context without exceeding token limits.

Three layers

Layer 1 — Active

Current conversation messages. Always in the context window.

Layer 2 — Summaries

Compressed summaries of completed tasks. Kept in memory.

Layer 3 — Full Storage

Complete output saved to disk. Retrieved on demand.

Setup

context.py
from veska import ContextManager

context = ContextManager(
    agent_id="researcher",
    storage_dir="./context",           # Where to save Layer 3
    max_summaries_in_context=20,       # Max Layer 2 summaries in window
)

Completing tasks

When a task finishes, save it to Layer 2 and 3:

complete.py
context.complete_task(
    task_id="task_001",
    summary="Researched AI agent frameworks and found 5 main options",
    key_facts=["Veska is the newest", "Most frameworks use LangChain"],
    files_created=["/reports/ai-agents.md"],
    decisions=["Focus on Python frameworks only"],
    connections=["Related to task_002 on ML tools"],
    full_output="... full research output text ...",
)

Building context

build.py
# Build a context string for the current task
context_str = context.build_context(current_task="Write the comparison section")
# Returns a formatted string with relevant summaries and history

# Retrieve full output from Layer 3
full = context.retrieve_full("task_001")

# Get all summaries
summaries = context.get_summaries()

# Trim messages to fit window
trimmed = context.trim_messages(messages, max_messages=50)
# Keeps system message + most recent messages