# Aperture Context Engineering

> Aggressively shrink LLM context windows by 20-66% while pinning tasks, constraints, and searchable history.

Aperture Context Engineering is a paid AI agent skill on Agensi by Saqib Mehdi, in Prompt & Skill Engineering. It loads into your AI agent through the Agensi MCP server, always in its latest approved version.

- Price: $19 (one-time purchase)
- Creator: [Saqib Mehdi](https://www.agensi.io/creators/saqib-mehdi)
- Category: [Prompt & Skill Engineering](https://www.agensi.io/skills/prompt-skill-engineering)
- Version: 1.0
- Last updated: 2026-12-01
- Security: scan passed (2026-12-01); every new listing is also reviewed by a person
- Listing: https://www.agensi.io/skills/aperture-context-engineering
- Tags: prompt-engineering, ai-agents, python, token-optimization, context-window, prompt-caching, token-cost, claude

## About this skill

### Aperture: Guardrailed Context Curation for LLM Agents

#### The Problem

In long-running agent sessions, **60–90% of accumulated tokens can come from tool outputs** that are read once and never referenced again.

Naive truncation is dangerous for two reasons:

##### 1. Dropped facts can cause silent fabrication

When relevant facts are removed from the context, models may guess plausible answers instead of acknowledging that the information is unknown.

In the author's recorded live runs:

- A Gemini model guessed `4098` for a vault code.
- A Groq model repeated filler text verbatim.

##### 2. Dropped constraints can cause governance decay

Removing constraints during compaction can weaken an agent's adherence to them.

Published research ([arXiv:2606.22528](https://arxiv.org/abs/2606.22528), based on 1,323 episodes) reports prohibited-action rates rising from **0% to 30–59%** after compaction drops a constraint.

---

#### What Aperture Does

Aperture adds **guardrailed context curation** to LLM agent loops that accumulate conversation history or tool outputs.

It provides three integration modes:

##### 1. One-shot mode

Call `curate_messages()` before any model call.

- Designed to fail open.
- Curation errors cannot break the agent loop.
- If curation fails, the original message list is returned unchanged.

##### 2. Loop mode

Use the `Curator` with:

- **Survival probes** to check whether important information remains available.
- **Sticky evidence** to protect critical facts and constraints.
- A **JSONL trace ledger** that makes every drop, elision, and re-admission decision observable.

##### 3. Advisory mode

Audit an existing transcript and estimate potential token savings before changing any code.

---

#### Reproducible Evaluation

Aperture includes a deterministic evaluation suite with:

- **5 evaluation arms**
- **10 task families**
- **2 model behaviors**
- **6 languages:** English, Chinese, Spanish, Arabic, Hindi, and Urdu

##### Reported results

| Approach | Task success | Token savings |
|---|---:|---:|
| Aperture | 1.00 across every task family | 20–66% |
| Naive truncation | 0.00 across every task family | — |

##### Prompt-injection defense

In the reported injection-defense evaluation:

- Both Aperture arms refuse a `SYSTEM OVERRIDE` planted in a tool output.
- All three baseline approaches obey the injected instruction.

##### Test coverage

The project reports:

- **152 unit tests** passing.
- **10 wire-shape checks** against the real adapter.
- An independent **16-battery stress suite** covering:
  - Cache economics
  - Multilingual recall
  - Prompt injection
  - Protocol handling

To reproduce the evaluation and regenerate the reported numbers, run:

```bash
python scripts/run_eval.py
```

---

#### Security and Reliability

Aperture is designed with a conservative security posture:

- Uses Python's standard library only; no pip installations required.
- Makes no network calls.
- Does not harvest environment variables.
- Does not access secrets.
- Uses no obfuscation.
- Fails open by design.

**Fail-open behavior:** If any error occurs during curation, Aperture returns the original message list unchanged and reports the failure. The curation layer is designed not to interrupt the agent loop.

---

#### What Is Included

The package provides:

- The complete runtime: `aperture_lite`
- The evaluation suite
- 152 unit tests
- Integration patterns for agent frameworks
- A sample transcript
- Updates for the **0.8 version line**

---

#### In Short

Aperture helps agent systems reduce context overhead without blindly discarding information that may be essential for correctness or policy compliance. It combines context curation with evidence preservation, survival checks, and an observable decision trail.

Run the evaluation yourself:

```bash
python scripts/run_eval.py
```

## Requires

- Python
- Git

## Known limitations

Character-based token estimation requires calibration for non-English scripts. Deterministic results are based on provided synthetic tasks; user workloads should be audited with the audit script.

## How to use this skill

1. Connect your AI agent to Agensi once: add the MCP server https://mcp.agensi.io/mcp/auth and sign in with your Agensi account (OAuth, no API key). Steps for Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and other agents: https://www.agensi.io/docs/mcp-setup
2. Ask your agent: "Use the Aperture Context Engineering skill from Agensi" and describe your task.
3. Buy it on the listing or let your agent ask you first; your agent then loads it from Agensi when it needs it. Every paid skill has a money-back guarantee: https://www.agensi.io/docs/money-back-guarantee

## Frequently asked questions

### How does this skill prevent an AI agent from forgetting its original task during long conversations?

This skill prevents goal drift and silent failures in long sessions by pinning system prompts and task statements while automatically restoring dropped facts from an archive when the model needs them. It also protects against injection attacks by marking untrusted tool output and maintains provider cache stability to keep costs low.

### What specific components are included for managing an agent's memory?

The skill provides a dependency-free Python runtime that includes a curation engine, a searchable archive for dropped history, and adapters for major message formats like OpenAI, Anthropic, Gemini, and others. You also get a trace ledger for observability and a transcript auditing script to project potential savings from your existing logs.

### How do I integrate these context management features into my agent's workflow?

You can provide your agent with a list of messages to be curated or use the built-in Curator object to manage ingestion and survival probes in a loop. For more autonomy, you can bind a recall tool to the agent so it can manually search the archive for any history that was removed to save space.

### Are there any limitations to how the skill calculates token usage?

The skill uses a character-based token estimator that varies by script, such as roughly one token per character for CJK and two for Arabic or Devanagari. Users should calibrate their specific token budgets against real provider telemetry for the highest accuracy.

### What software or libraries are required to run these tools?

This skill is written in standard library Python and requires no external dependencies. It works with any agent loop that can process message lists or tool outputs in the supported provider formats like Bedrock Converse, Cohere, or Ollama.

### How can I instruct my AI agent to start optimizing its own context window?

Ask your agent to use the curate_messages function to fit a conversation into a specific token budget or to run the audit_transcript script on your logs to identify context-health risks. You can also tell it to register survival probes for critical safety constraints to ensure they never leave the active window.

---

Source: [Agensi](https://www.agensi.io), the marketplace of skills for AI agents. Any MCP-compatible agent (Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and more) connects at https://mcp.agensi.io/mcp/auth and signs in with an Agensi account; setup for every agent: https://www.agensi.io/docs/mcp-setup. Append .md to any skill, guide or docs address on agensi.io for its markdown version.
