Skip to content

Recognise gpt-6 model ids (gpt-6-luna, gpt-6-sol) for OpenAI cache options - #33

Open
mindful-time wants to merge 1 commit into
lm15-dev:mainfrom
mindful-time:fix/gpt-major-only-cache-options
Open

mindful-time wants to merge 1 commit into
lm15-dev:mainfrom
mindful-time:fix/gpt-major-only-cache-options

Conversation

@mindful-time

@mindful-time mindful-time commented Nov 22, 2026 •

Copy link
Copy Markdown

Problem

openai_model_has_cache_options() only recognises ids shaped like gpt-5.6-sol. The gpt-6 ids have no minor version (gpt-6-luna, gpt-6-sol, gpt-6-astra), so lm15 treats them as old models.

Result: on gpt-6, a cached request sends the breakpoint but not prompt_cache_options: {"mode": "explicit"}, so OpenAI writes the whole prompt to cache at 1.25x on every call.

Measured in stanfordnlp/dspy#10658 on gpt-6-luna: 14,780 tokens written instead of 9,899.

Fix

Also accept gpt-<one digit> followed by - or end of string, read as <major>.0.

Model id Before After
gpt-5.6-sol ✅ ✅
gpt-6-luna, gpt-6-sol, gpt-6 ❌ ✅
gpt-5, gpt-5-mini, gpt-5.4-mini, gpt-4o ❌ ❌
gpt-35-turbo (Azure) ❌ ❌ (one digit only, so not "35")

The existing gpt-<major>.<minor> check is unchanged. This covers both OpenAI dialects (Responses and Chat Completions), since both use this check.

Tests

  • Added the ids above to test_openai_model_class_for_cache_options.
  • Added request-body checks for gpt-6-luna on Responses (prefix="stable", mode="off") and Chat Completions.
  • pytest -q: 3895 passed. Conformance (strict) and the contract harness at CONTRACT_PIN pass. The only exception is 13 auth cases that fail identically on main locally, because of cloud credentials on my machine.

No contract change is needed: no contract case uses a gpt-6 id, and the caching design note already expected this check to be updated for new name patterns.

Supersedes the closed #22. Refs stanfordnlp/dspy#10658.

🤖 Generated with Claude Code

… as 6.0

openai_model_has_cache_options parsed only gpt-<major>.<minor>, so the
gpt-6 family (gpt-6-luna, gpt-6-sol, gpt-6-astra) fell outside the 5.6+
class. A prefix="stable" or prefix_until_index request still carried the
prompt_cache_breakpoint but not prompt_cache_options mode=explicit, so the
provider also wrote the volatile suffix at 1.25x on every call, and
mode="off" sent no off switch. Measured on gpt-6-luna (DSPy #10658): 14,780
tokens written instead of 9,899 up to the mark.

A gpt-<digit> id followed by "-" or the end now reads as <major>.0. One
digit only, so Azure's gpt-35-turbo deployment names stay out of the class.
The gpt-<major>.<minor> parse is unchanged.

Refs: stanfordnlp/dspy#10658

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Mindfultime <mindfultime@outlook.com>
@mindful-time mindful-time changed the title OpenAI cache options for major-only GPT ids (MAP-6): gpt-6-luna reads as 6.0 Recognise gpt-6 model ids (gpt-6-luna, gpt-6-sol) for OpenAI cache options Nov 22, 2026
@mindful-time

Copy link
Copy Markdown
Author

Verification run

Every request path, before and after this change. The table shows whether the request body includes prompt_cache_options: {"mode": "explicit"}. All rows send prompt_cache_breakpoint when prefix="stable".

                 BEFORE (main @ 1ae9820)              AFTER (this PR @ 31531fb)
model            stable    off       chat             stable    off       chat
gpt-5.6-luna     explicit  explicit  explicit         explicit  explicit  explicit
gpt-6-luna       -         -         -           →    explicit  explicit  explicit   fixed
gpt-6-sol        -         -         -           →    explicit  explicit  explicit   fixed
gpt-6-astra      -         -         -           →    explicit  explicit  explicit   fixed
gpt-5.4-mini     -         -         -                -         -         -          unchanged (pre-5.6)
gpt-35-turbo     -         -         -                -         -         -          unchanged (Azure 3.5)

stable = OpenAILM + CacheConfig(prefix="stable"), off = OpenAILM + CacheConfig(mode="off"), chat = OpenAIChatLM + CacheConfig(prefix="stable").

Script (extends the repro from stanfordnlp/dspy#10658)
import json
from lm15.providers.openai import OpenAILM
from lm15.providers.openai_chat import OpenAIChatLM
from lm15.types import CacheConfig, Config, Message, Request

def body(lm, model, cache):
    r = Request(model=model, system="rules", messages=(Message.user("question"),), config=Config(cache=cache))
    return json.loads(lm(api_key="x").build_request(r, stream=False).body)

for m in ("gpt-5.6-luna", "gpt-6-luna", "gpt-6-sol", "gpt-6-astra", "gpt-5.4-mini", "gpt-35-turbo"):
    f = lambda b: "explicit" if b.get("prompt_cache_options") else "-"
    print(m, f(body(OpenAILM, m, CacheConfig(prefix="stable"))),
             f(body(OpenAILM, m, CacheConfig(mode="off"))),
             f(body(OpenAIChatLM, m, CacheConfig(prefix="stable"))))

Tests

Run Result
pytest tests/test_cache_mapping.py, this PR 17 passed
same, with openai.py reverted to main 4 failed (the new gpt-6 checks), 13 passed
pytest -q, full suite 3895 passed, 8 skipped
conformance/run_all.py --strict OK
lm15-contract/harness/check.py --shim python --direction all @ CONTRACT_PIN request 539/0, cache 11/0, mapping 2145/0 (pass/fail). 13 auth failures happen identically on main locally, because of cloud credentials on my machine

Downstream (DSPy)

DSPy vendors lm15 2.2.0. This PR's diff applies cleanly to dspy/_vendor/lm15/providers/openai.py:

  • The table above, run through dspy._vendor.lm15, goes from the BEFORE columns to the AFTER columns.
  • DSPy's tests/clients/ passes with the patch: 1159 passed, 62 skipped.

All runs use the fake transport, with no live API calls. The billing effect (9,899 vs 14,780 tokens written on gpt-6-luna) is the live measurement reported in stanfordnlp/dspy#10658.

@mindful-time

Copy link
Copy Markdown
Author

Live check against the real OpenAI API

Result: with this PR, gpt-6-luna gets prompt_cache_options: {"mode": "explicit"}, and OpenAI caches only the stable prefix, the same way it already does for gpt-5.6-luna.

Setup: two calls per model. Both calls share a ~15k-token system prompt marked CacheConfig(prefix="stable"), and each call has a different ~6k-token user message. Each run uses a fresh random tag, so the cache starts cold.

gpt-6-luna (the bug)

Call Mode sent Written to cache Read from cache
Before (main 1ae9820) 1 none 22,657: the whole prompt 0
2 none 7,758: the new user message, written again 14,899
After (this PR) 1 explicit 16,141: the shared prefix only 0
2 explicit 0 16,141

Cache writes cost 1.25x input. Over two calls, the tokens written fall from 30,415 to 16,141.

gpt-5.6-luna (control: already worked)

Call Mode sent Written to cache Read from cache
Before and after (same numbers) 1 explicit 14,278 0
2 explicit 0 14,278

The two runs give the same numbers, so models that already worked are unaffected.

Script used (run with PYTHONPATH pointed at the checkout under test)
"""Live check for stanfordnlp/dspy#10658 / lm15-python#33.

Usage: python live_e2e.py <key-file> <label> <model> [<model> ...]
Run with PYTHONPATH pointing at the lm15 checkout under test.

Per model: a ~10k-token system prompt (fresh nonce, so the cache starts cold)
marked with prefix="stable", followed by a ~5k-token user message. Two calls
share the system prompt and differ in the user message.
  explicit mode -> call 1 writes ~prefix only, call 2 writes ~0
  implicit mode -> call 1 writes the whole prompt, call 2 writes the new suffix
"""
import json
import secrets
import sys
import time

from lm15.providers.openai import OpenAILM
from lm15.types import CacheConfig, Config, Message, Request

key_file, label, models = sys.argv[1], sys.argv[2], sys.argv[3:]
lm = OpenAILM(api_key=open(key_file).read().strip())

def filler(tag: str, n: int) -> str:
    return " ".join(f"Rule {tag}-{i}: record item {i} in ledger {i % 97} before noon." for i in range(n))

for model in models:
    nonce = secrets.token_hex(8)
    system = f"[{nonce}] You answer with one word.\n" + filler("S" + nonce, 620)
    rows = []
    for call in (1, 2):
        user = filler(f"U{call}{nonce}", 310) + "\nReply with: ok"
        req = Request(model=model, system=system, messages=(Message.user(user),),
                      config=Config(cache=CacheConfig(prefix="stable"), max_tokens=64))
        body = json.loads(lm.build_request(req, stream=False).body)
        try:
            u = lm.complete(req).usage
        except Exception as e:  # model access, quota, etc.
            print(f"{label:6} {model:13} call {call}: ERROR {type(e).__name__}: {str(e)[:200]}")
            break
        rows.append((call, body.get("prompt_cache_options"), u.input_tokens, u.cache_write_tokens, u.cache_read_tokens))
        time.sleep(2)
    for call, opts, inp, w, r in rows:
        mode = "explicit" if opts else "-"
        print(f"{label:6} {model:13} call {call}: mode={mode:8} input={inp:6} written={w!s:6} read={r!s:6}")

🤖 Generated with Claude Code

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant