Skip to content

Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.

video: announce the per-call charge before a clip leaves on XAI_API_KEY

Soohong Kim13e8e8e4d091
diff --git a/sprite_gen/gen/video.py b/sprite_gen/gen/video.py
index 7bc6644..9eadbe5 100644
--- a/sprite_gen/gen/video.py
+++ b/sprite_gen/gen/video.py
@@ -21,7 +21,9 @@ resolved in a fixed order and always reported (`auth_source`):
1. the grok CLI login file `~/.grok/auth.json` (SuperGrok Imagine quota via the
OIDC access token the CLI stored at `grok login`). `GROK_HOME` relocates it.
-2. `XAI_API_KEY` - an xAI console key, only when no grok login file exists.
+2. `XAI_API_KEY` - an xAI console key, only when no grok login file exists. That
+ route spends metered API credit, so it says so on stderr before it uploads;
+ the subscription route stays silent (구독 우선 불변식 5, 수홍 2026-09-20).
The login token expires (about six hours, 2026-09-08 실측) and the grok CLI is the
only writer of that file, so an expired token is not refreshed here: the run
@@ -53,6 +55,7 @@ from pathlib import Path
from typing import Any, Callable
from sprite_gen.spec.runio import atomic_write_text
+from .base import announce_api_billing
from .xai import (
API_BASE, AUTH_ENV, AUTH_SOURCE_API_KEY, AUTH_SOURCE_GROK_LOGIN,
HTTP_TIMEOUT_SECONDS, GROK_REFRESH_COMMAND, GROK_REFRESH_WHERE, GROK_LOGIN_COMMAND,
@@ -365,7 +368,21 @@ def _submit_poll_publish(
`accept(staged_path, final_poll)` may refuse the clip (raise SystemExit) while it is
still the `.part` file — `video-extend` measures the staged bytes with ffprobe; the
- staged file is removed and `out` is never written."""
+ staged file is removed and `out` is never written.
+
+ Every billed request leaves from here, which is why the API-credit notice lives
+ here too: one line per submitted job, whichever verb asked for it."""
+ if credential.source == AUTH_SOURCE_API_KEY:
+ # Reaching the key means no grok login exists (resolve_credential's fixed
+ # order), so this clip spends metered API credit instead of the Imagine
+ # subscription quota. What sets the amount travels with the charge: Imagine
+ # prices a clip by length x output size, so a 15 s 1080p clip is nothing
+ # like a 3 s 480p one (`video-edit` sends neither and names neither).
+ priced = ", ".join(f"{knob}={body[knob]}{'s' if knob == 'duration' else ''}"
+ for knob in ("duration", "resolution") if knob in body)
+ announce_api_billing(verb, AUTH_ENV,
+ f"{f' ({priced})' if priced else ''} — "
+ f"`{GROK_LOGIN_COMMAND}` signs your Grok subscription in.")
started = time.monotonic()
status, reply = call("POST", f"{API_BASE}/videos/{endpoint}", credential.token, body)
if status in (401, 403):
diff --git a/tests/gen/test_video.py b/tests/gen/test_video.py
index 4e131f8..da0e4f2 100644
--- a/tests/gen/test_video.py
+++ b/tests/gen/test_video.py
@@ -342,3 +342,88 @@ def test_refresh_prescription_is_a_non_agent_command_run_outside_the_repo(tmp_pa
assert video.GROK_REFRESH_WHERE in message
assert "grok -p ok" not in message.replace("`grok -p …`", "")
assert "coding agent" in message
+
+
+# --- the API-credit notice (구독 우선 불변식 5) ---------------------------------
+
+
+def _billed_run(verb: str, tmp_path: Path, **transport):
+ """Submit one job of `verb` through the fake transport."""
+ if verb == "video":
+ return video.generate_video(_request(tmp_path, duration=3, resolution="480p"), **transport)
+ clip = tmp_path / "in.mp4"
+ clip.write_bytes(MP4)
+ transport = {**transport, "probe": lambda path: 6.0} # ffprobe stand-in: input and staged alike
+ if verb == "video-extend":
+ return video.extend_video(video.ExtendRequest(video=clip, prompt="she presses on", out=tmp_path / "longer.mp4", duration=4), **transport)
+ return video.edit_video(video.EditRequest(video=clip, prompt="make the hakama black", out=tmp_path / "edited.mp4"), **transport)
+
+
[email protected]("verb, priced", [
+ ("video", " (duration=3s, resolution=480p)"),
+ ("video-extend", " (duration=4s)"),
+ ("video-edit", ""), # the edit body sends neither knob, so the line names neither
+])
+def test_every_verb_announces_the_charge_before_the_request_leaves(tmp_path: Path, capsys, verb, priced) -> None:
+ """A clip on XAI_API_KEY is metered API credit; the payer hears it before the upload,
+ not on the invoice. Imagine prices a clip by length x output size, so the knobs that
+ set the amount ride along with the charge."""
+ api = _FakeApi()
+ heard: list[str] = []
+
+ def call(method, url, token, body):
+ if method == "POST":
+ heard.append(capsys.readouterr().err) # everything said before the job was submitted
+ return api.call(method, url, token, body)
+
+ _billed_run(verb, tmp_path, credential=video.Credential("console-key", video.AUTH_SOURCE_API_KEY),
+ call=call, download=api.download, sleep=lambda s: None)
+
+ notice = [line for line in heard[0].splitlines() if "per-call API charge" in line]
+ assert len(notice) == 1
+ assert notice[0].startswith(f"[gen] {verb}: running on {video.AUTH_ENV} ")
+ assert f"not a subscription{priced} — " in notice[0]
+ # The shared prefix denies the subscription once; the detail carries the remedy only
+ # (the grok image notice read "not a subscription (...), not your Grok subscription").
+ assert notice[0].count("not ") == 1
+ assert f"`{video.GROK_LOGIN_COMMAND}` signs your Grok subscription in." in notice[0]
+
+
[email protected]("verb", ["video", "video-extend", "video-edit"])
+def test_the_subscription_route_stays_silent_about_billing(tmp_path: Path, capsys, verb) -> None:
+ api = _FakeApi()
+ _billed_run(verb, tmp_path, credential=video.Credential("tok", video.AUTH_SOURCE_GROK_LOGIN),
+ call=api.call, download=api.download, sleep=lambda s: None)
+ assert "per-call API charge" not in capsys.readouterr().err
+
+
+def test_a_request_that_never_leaves_never_announces_a_charge(tmp_path: Path, monkeypatch, capsys) -> None:
+ """The line tracks charges, not intentions: a body refused locally costs nothing."""
+ monkeypatch.setenv("GROK_HOME", str(tmp_path / "no-login"))
+ monkeypatch.setenv("XAI_API_KEY", "console-key")
+ api = _FakeApi()
+ with pytest.raises(SystemExit, match="--duration must be"):
+ video.generate_video(_request(tmp_path, duration=99), call=api.call, download=api.download, sleep=lambda s: None)
+ assert api.calls == []
+ assert "per-call API charge" not in capsys.readouterr().err
+
+
+def test_the_cli_announces_on_stderr_leaving_the_report_on_stdout(tmp_path: Path, monkeypatch, capsys) -> None:
+ """The credential the environment actually hands the CLI is what decides, and the
+ notice goes to stderr so a caller parsing stdout still reads one JSON report."""
+ monkeypatch.setenv("GROK_HOME", str(tmp_path / "no-login"))
+ monkeypatch.setenv("XAI_API_KEY", "console-key")
+ api = _FakeApi()
+ monkeypatch.setattr(video, "http_json", api.call)
+ monkeypatch.setattr(video, "http_download", api.download)
+ monkeypatch.setattr(video.time, "sleep", lambda s: None)
+
+ rc = video.run(image=_still(tmp_path), prompt="sway", prompt_file=None, out=tmp_path / "clip.mp4",
+ duration=6, resolution="720p", aspect_ratio=None, model=video.DEFAULT_MODEL,
+ generate_audio=None, report=None)
+
+ captured = capsys.readouterr()
+ assert rc == 0
+ assert len([line for line in captured.err.splitlines() if "per-call API charge" in line]) == 1
+ assert json.loads(captured.out)["auth_source"] == "XAI_API_KEY"
+ assert "console-key" not in captured.err + captured.out # the key itself is never printed