Prompt Basics 01 / 06

what is a prompt.

A prompt isn't just the single line you type. AI models are stateless, meaning they remember nothing between turns. Every time you hit send, your app bundles hidden rules, tool definitions, your entire chat history, and your new message into one giant sequence of tokens.

Anatomy of one agent turn (click any block to inspect)
TURN 12 · 50,400 TOKENS SENT TO MODEL processed left to right
SYSTEM PROMPT · 16% OF CONTEXT

Standing instructions that define the assistant's role, tone, safety rules, and output formatting. Sent invisibly at the top of every single request.

You are a senior software engineer helping debug a TypeScript codebase.
Always cite exact file paths and explain root causes concisely.
8,000
tokens
Because the model has no memory, 99% of every request is text you already sent before. TURN 12
0
Built-in memory between API calls
~99%
Repeated tokens on a typical agent turn
100%
Re-billed every turn without caching
Agent Costs 101 02 / 06

wtf is prompt caching.

Every turn, your agent sends the model the same 50,000 tokens: the system prompt, the tool definitions, and the chat so far. Prompt caching saves that work in fast memory, so you stop paying full price to process it again.

Every turn, uncached $0.15
You pay full price for all 50,000 tokens every single turn.
Every turn, cached $0.0075
The repeated 50,000 tokens cost 5% of normal. Only the new message is full price.
Cached tokens Full-price tokens
20×
cheaper on every turn after the first
$15.00
100 turns, uncached
$0.89
100 turns, cached
How It Works 03 / 06

The cache only works if the start of your prompt stays the same.

The provider compares your new request with the last one, starting from the first token. Everything up to the first difference comes from the cache. Everything after it is billed at full price.

ONE TURN, 50,000 TOKENS · CLICK WHERE A CHANGE HAPPENS compared from the start
Cached: unchanged since last turn (50,000 tokens hit cache) Turn cost: $0.0075
What a session looks like
Turn 1
$0.15
Turn 2
$0.0075
Turn 3
$0.0075

Turn 1 pays full price and fills the cache. After that, you only pay full price for the latest reply and your new message.

Put the parts that never change at the top.
01

System prompt and tools first. They stay the same every turn.

02

Chat history next. Add new messages to the end and leave old ones alone.

03

Anything that changes each turn, like the date or time, goes in the newest message.

The Catch 04 / 06

Rewrite the system prompt mid-session and the cache is gone.

Turn 12
$0.0075
Turn 13
$0.15
Turn 14
$0.0075

Change the first line and the very first token no longer matches, so all 50,000 tokens are billed at full price. That turn costs 20× more than the one before it. Turn 14 is cached again, but if you edit the prompt every turn, you never get the discount.

Breaks the cache

  • Editing the system prompt
  • Putting the date or time in the system prompt
  • Adding, removing or reordering tools
  • Summarizing or trimming old messages
  • Switching models mid-session
  • Waiting too long between turns (caches expire)

Keeps it warm

  • Write the system prompt once and leave it
  • Send the date in the latest message instead
  • Load all your tools at the start, in the same order
  • Only add new messages to the end
  • Use one model for the whole session
  • Keep turns close together
Simple rule: once you send something, don't change it. Only add to the end.
Interactive Simulator 05 / 06

See what a session costs.

Each bar is one turn of an agent session (50,000 tokens). Click any bar to edit the system prompt on that turn.

Cache hit · $0.0075 Full price · $0.15 System prompt edited
$0.15 $0.00
Turn 1 Turn 40
Without caching
$6.00
This session
$0.44
Saved
93%
Extra cost from edits
$0.00
Official Resources 06 / 06

Go deeper with official guides and videos.

Ready to implement prompt caching in production? Watch these technical walkthroughs and explore the official provider documentation for exact token floors, TTL windows, and SDK examples.

Featured YouTube Walkthroughs

Prompt Caching Explained: Stop Overpaying for AI Agents by Hugging Face 17:16
Hugging Face Official Video

Prompt Caching Explained: Stop Overpaying for AI Agents

Covers why multi-turn agent sessions get expensive, what prompt caching actually stores, provider pricing differences, and harness mistakes that break your prefix.

Watch on YouTube
What is Prompt Caching? Optimize LLM Latency with AI Transformers by IBM Technology 09:06
IBM Technology Official Video

What is Prompt Caching? Optimize LLM Latency with AI Transformers

Explains how transformer key-value (KV) caching eliminates redundant prefill computation to cut both time-to-first-token latency and input costs.

Watch on YouTube

Official Documentation & Articles

Google Gemini API

Context caching in the Gemini API

Official guide to implicit caching (up to 95% input savings on Gemini 4 with no storage fees) and prompt-prefix engineering.

Anthropic Docs

Prompt caching on the Claude Platform

Covers automatic caching, explicit cache_control breakpoints, minimum token thresholds, and multi-turn conversation patterns.

OpenAI API Docs

Prompt caching guide for the OpenAI API

Explains how key-value (KV) tensor prefixes are cached automatically across requests and how to monitor hit rates.

Anthropic Engineering

Prompt caching with Claude: Overview & benchmarks

Deep dive into real-world latency and cost reductions for coding assistants, large document Q&A, and long agent sessions.