Skip to content

Phase 8: Context Management — token counting, compaction, CLAUDE.md, memory #67

@gHashTag

Description

@gHashTag

Problem

tri-api accumulates messages with no size management. After ~5-10 tool-heavy turns, context exceeds Claude's 200K token limit → API returns 400 → session dies.

Solution: 3 new files + 2 modified (~300 LOC)

P0 — Context Survival

  • context.zig (~150 LOC): Token estimation (bytes/4), two-phase compaction:
    1. Truncate old tool outputs (keep last 3 turns)
    2. Summarize via API call (if still over 80%)

P1 — Instructions Layer

  • claude_md.zig (~80 LOC): Load CLAUDE.md hierarchy → system prompt
  • memory.zig (~60 LOC): Persistent learnings in ~/.tri-api/MEMORY.md

Wiring

  • main.zig (+30 LOC): Compaction before each turn, system prompt in API request
  • tool_protocol.zig (+5 LOC): Support for system field

Acceptance Criteria

  • Token estimation works
  • Compaction triggers at 80% of 180K tokens
  • Tool output truncation preserves last 3 turns
  • CLAUDE.md loaded from hierarchy
  • System prompt injected into API request
  • Memory loads from ~/.tri-api/MEMORY.md
  • TUI shows context usage per turn
  • zig build clean, all tests pass

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions