prompt-caching-and-how-to-think-about-it

v1v2 · +94 −29 · View latest

⋯ 303 unchanged lines
        min-width: 0;
      }
      .visual-split {
        display: grid;
        grid-template-columns: minmax(130px, 0.8fr) minmax(0, 1.35fr);
        gap: 10px;
        align-items: stretch;
      }
      .visual-pane {
        min-width: 0;
        padding: 10px;
        border: 1px solid var(--soft-line);
        border-radius: var(--radius);
        background: #fbf9f4;
      }
      .pane-title {
        margin: 0 0 8px;
        color: var(--muted);
        font-size: 0.66rem;
        font-weight: 800;
        text-transform: uppercase;
      }
      .chat-box {
        display: grid;
        gap: 7px;
        margin-bottom: 12px;
      }
      .bubble {
⋯ 307 unchanged lines
          position: sticky;
          top: 0;
          z-index: 10;
          max-height: 34vh;
          overflow-y: auto;
          border-radius: 0 0 var(--radius) var(--radius);
          margin: 0 -20px 22px;
          border-left: 0;
          border-right: 0;
        }
        .visual-split {
          grid-template-columns: minmax(120px, 0.9fr) minmax(0, 1.4fr);
        }
        .visual-top {
          margin-bottom: 8px;
        }
        .rail {
          min-height: 300px;
          min-height: 230px;
        }
        .scene {
⋯ 20 unchanged lines
        }
        .timeline {
          grid-template-columns: 18px minmax(0, 1fr);
          gap: 7px;
          grid-template-columns: minmax(0, 1fr);
        }
        .rail::before {
          left: 8px;
        .rail {
          display: none;
        }
        .rail-dot {
          left: 3px;
          width: 12px;
          height: 12px;
        .visual-split {
          grid-template-columns: minmax(82px, 1fr) minmax(0, 2fr);
          gap: 8px;
        }
        .visual-pane {
          padding: 8px;
        }
        .pane-title {
          margin-bottom: 5px;
          font-size: 0.58rem;
        }
        .chat-box {
          max-height: 96px;
          overflow-y: auto;
        }
        .bubble {
          padding: 5px 7px;
          font-size: 0.68rem;
        }
        .visual-note {
          display: none;
        }
        .hash-row {
          margin-top: 8px;
        }
        .request-bar {
          min-height: 42px;
        }
⋯ 39 unchanged lines
              <p class="lane-label">Tiny chat request</p>
              <div class="request-bar">
                <div class="segment system" style="flex-basis: 58%">
                  launch strategist prompt
                  system prompt
                </div>
                <div class="segment message" style="flex-basis: 42%">
                  user message
⋯ 26 unchanged lines
          <span class="section-kicker">Request 1</span>
          <h2>The first request writes the cache.</h2>
          <p>
            The launch strategist prompt is the stable part. You expect to send
            it again and again, so you mark it with Anthropic's cache control.
            The system prompt is the stable part. You expect to send it again
            and again, so you mark it with Anthropic's cache control.
          </p>
          <pre><code>system = [{
    "type": "text",
    "text": "You are a launch strategist...",
    "text": "System prompt: help plan product launches...",
    "cache_control": {"type": "ephemeral", "ttl": "1h"}
}]</code></pre>
          <p>
⋯ 7 unchanged lines
          <h2>The second request reuses the same prefix.</h2>
          <p>
            The conversation changed, but the cached prefix did not. The marker
            is still after the same launch strategist prompt, so Claude can read
            that part from cache.
            is still after the same system prompt, so Claude can read that part
            from cache.
          </p>
          <div class="callout">
            A cache hit means the prefix matches at a cache marker. Not similar
⋯ 15 unchanged lines
            a different cache.
          </p>
          <div class="callout">
            Same launch strategist prompt. Different prefix. Different cache.
            Same system prompt. Different prefix. Different cache.
          </div>
        </section>
⋯ 176 unchanged lines
          <div class="timeline">
            <div class="rail" id="rail" aria-hidden="true"></div>
            <div class="visual-stage">
              <div class="chat-box" id="chatBox"></div>
              <div class="request-stack" id="requestStack"></div>
              <div class="visual-split">
                <section class="visual-pane chat-pane" aria-label="Chat conversation">
                  <p class="pane-title">Chat</p>
                  <div class="chat-box" id="chatBox"></div>
                </section>
                <section class="visual-pane request-pane" aria-label="Request structure">
                  <p class="pane-title">Request structure</p>
                  <div class="request-stack" id="requestStack"></div>
                </section>
              </div>
              <div class="hash-row" id="hashRow"></div>
              <p class="visual-note" id="visualNote"></p>
              <div class="controls" id="controls"></div>
⋯ 28 unchanged lines
              label: "Request",
              marker: 0,
              segments: [
                ["system", "launch strategist prompt", 58, "system"],
                ["system", "system prompt", 58, "system"],
                ["message", "user message", 42, "message"],
              ],
            },
          ],
        },
        {
          id: "write",
          title: "First request",
          subtitle: "The stable launch strategist prompt is written to cache.",
          subtitle: "The stable system prompt is written to cache.",
          note: "The first matching request writes cache A. Later calls can reuse it.",
          hash: [
            { text: "hash A", tone: "" },
⋯ 5 unchanged lines
              label: "Request 1",
              marker: 58,
              segments: [
                ["system write", "launch strategist prompt", 58, "system"],
                ["system write", "system prompt", 58, "system"],
                ["message", "user message", 42, "message"],
              ],
            },
⋯ 18 unchanged lines
              label: "Request 2",
              marker: 42,
              segments: [
                ["system read", "launch strategist prompt", 42, "system"],
                ["system read", "system prompt", 42, "system"],
                ["message", "user", 17, "message"],
                ["message", "assistant", 22, "message"],
                ["message", "user", 19, "message"],
              ],
            },
          ],
        },
        {
          id: "tool",
          title: "Add a tool",
          subtitle: "Tools come before the launch strategist prompt in the prefix.",
          note: "Same launch strategist prompt, different request shape, different cache.",
          subtitle: "Tools come before the system prompt in the prefix.",
          note: "Same system prompt, different request shape, different cache.",
          hash: [
            { text: "before: hash A", tone: "read" },
            { text: "after: hash B", tone: "miss" },
⋯ 7 unchanged lines
              label: "Before tool",
              marker: 55,
              segments: [
                ["system read", "launch strategist prompt", 55, "system"],
                ["system read", "system prompt", 55, "system"],
                ["message", "messages", 45, "message"],
              ],
            },
            {
              label: "After tool",
              marker: 68,
              segments: [
                ["tool", "tool", 20, "tool"],
                ["system write", "launch strategist prompt", 48, "system"],
                ["system write", "system prompt", 48, "system"],
                ["message", "messages", 32, "message"],
              ],
            },
⋯ 14 unchanged lines
              label: "Normal chat lane",
              marker: 54,
              segments: [
                ["system read", "launch strategist prompt", 54, "system"],
                ["system read", "system prompt", 54, "system"],
                ["message", "messages", 46, "message"],
              ],
            },
            {
              label: "Structured lane",
              marker: 76,
              segments: [
                ["schema", "schema", 24, "schema"],
                ["system write", "launch strategist prompt", 52, "system"],
                ["system write", "system prompt", 52, "system"],
                ["message", "messages", 24, "message"],
              ],
            },
⋯ 442 unchanged lines