Repository navigation
Recognise gpt-6 model ids (gpt-6-luna, gpt-6-sol) for OpenAI cache options - #33
mindful-time wants to merge 1 commit into
Conversation
… 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>
Verification runEvery request path, before and after this change. The table shows whether the request body includes
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
Downstream (DSPy)DSPy vendors lm15 2.2.0. This PR's diff applies cleanly to
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. |
Live check against the real OpenAI APIResult: with this PR, Setup: two calls per model. Both calls share a ~15k-token system prompt marked
|
| 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
Problem
openai_model_has_cache_options()only recognises ids shaped likegpt-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.gpt-5.6-solgpt-6-luna,gpt-6-sol,gpt-6gpt-5,gpt-5-mini,gpt-5.4-mini,gpt-4ogpt-35-turbo(Azure)The existing
gpt-<major>.<minor>check is unchanged. This covers both OpenAI dialects (Responses and Chat Completions), since both use this check.Tests
test_openai_model_class_for_cache_options.gpt-6-lunaon Responses (prefix="stable",mode="off") and Chat Completions.pytest -q: 3895 passed. Conformance (strict) and the contract harness atCONTRACT_PINpass. The only exception is 13authcases that fail identically onmainlocally, 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