---
title: "Mastering Codex 1M Context Window & Prompt Caching: Maximize Cache Hit Rates in Long Sessions"
canonical_url: "https://tibosay.com/blog/codex-1m-context-and-prompt-caching-tuning"
type: "blog"
locale: "en"
description: "How Codex CLI leverages persistent WebSockets and prefix prompt caching across 1M context tokens — and the 4 habits that keep your cache hit rate above 90%."
published_at: "2026-10-27T02:00:00.000Z"
updated_at: "2026-10-27T02:00:00.000Z"
---

# Mastering Codex 1M Context Window & Prompt Caching: Maximize Cache Hit Rates in Long Sessions
> How Codex CLI leverages persistent WebSockets and prefix prompt caching across 1M context tokens — and the 4 habits that keep your cache hit rate above 90%.

## 1. How Codex Prompt Caching + WebSockets Work Together

In Day 12 through Day 16 of the 28-Day Challenge, @thsottiaux rolled out the **1M token context window** alongside **persistent WebSocket transport** and deeper prompt cache reuse. When your session prefix hits the warm cache, latency drops dramatically and quota burn per turn is a fraction of cold tokens.

## 2. Four Rules to Keep Cache Hit Rate Above 90%

- Do not edit AGENTS.md or system instructions mid-session : Changing the first 500 tokens invalidates the entire cached prefix for the rest of the conversation.

- Keep related tasks in the same repo session : Jumping across unrelated repositories wastes the warm file-tree and type-definition cache.

- Avoid switching models back and forth mid-thread : Each model family maintains its own KV cache pool; switching from Terra to Sol forces a cold prefill.

- Start a fresh session when switching to a brand-new feature : Carrying 300k tokens of finished UI work into a database migration task pays per-turn prefix costs for dead context.

> **Key Takeaway (TL;DR)**: Prompt caching discounts repeated prefix tokens by up to 75–90%, but only if the prefix stays byte-identical. Keep AGENTS.md static during a session, avoid switching models mid-thread, and compact before hitting context cliffs.

## Key Specifications

| Specification | Value |
| --- | --- |
| Max Context Window | 1,000,000 Tokens (1M) |
| Transport Protocol | Persistent Low-Latency WebSockets |
| Warm Cache Savings | Up to 75%–90% lower prefix cost |
| Cache Invalidation Trigger | Mutating system prompt / model switch |

## Frequently Asked Questions

### Q: Is it better to keep one giant 800k-token session all day or start new sessions?

Keep the same session while working on the same feature module so you benefit from warm cache hits, but start a fresh session as soon as you switch to an unrelated feature so you stop paying for old context.

## Related Resources

- [Codex 429 & Banked Reset Strategy](https://tibosay.com/blog/codex-429-quota-and-banked-reset-strategy)
- [AGENTS.md Token Optimization Playbook](https://tibosay.com/blog/codex-agents-md-token-optimization)
- [Live TiboSay Reset Radar & CLI](https://tibosay.com/cli)
