# 3.5 Mask, don't remove

Constrain the action space with logit masking; keep tool definitions byte-identical.

1. A state machine restricts the action space per phase.
2. Removing tool definitions invalidates the KV-cache.
3. Logit masking keeps the prefix byte-identical.
4. Phase transition: new mask, same cached prefix.

- **Use when:** Phase-gated agents with long cached prefixes.
- **Avoid:** Masking as the only authorization boundary: enforce server-side.

```
ALL_TOOLS = [search, read, edit, test, deploy]   # never changes

ALLOWED = {
    "research": ["search", "read"],
    "build":    ["read", "edit", "test"],
    "ship":     ["test", "deploy"],
}

llm.create(messages, tools=ALL_TOOLS,
           allowed_tools=ALLOWED[phase])         # only this changes
```

## References

- [Context engineering: lessons from building Manus](https://manus.im/blog/Context-Engineering-for-AI-Agents-Lessons-from-Building-Manus) (Manus)
- [Prompt caching](https://platform.claude.com/docs/en/build-with-claude/prompt-caching) (Anthropic)
- [Prompt caching](https://platform.openai.com/docs/guides/prompt-caching) (OpenAI)
