fix(grok): correct opencode provider (Responses API), stdin, reasoning cost

A live opencode run against api.x.ai/v1 surfaced three real bugs:

1. Provider package — grok-build-0.1 is driven via the OpenAI Responses API
   (opencode calls model.responses()). @ai-sdk/openai-compatible is
   chat/completions only and errors "responses is not a function". Switch the
   generated opencode.json provider + the grok image to @ai-sdk/openai.
2. Headless hang — `opencode run` blocks after init without a TTY; close stdin
   (`< /dev/null`) in the entrypoint so it proceeds to the model call.
3. Reasoning-token cost — grok-build-0.1 is a reasoning model; reasoning tokens
   bill as output but opencode stores them in a separate column. cost_for_session
   folds tokens_reasoning into output (else ~22x undercount).

Verified end-to-end against a real session row (input=6120, output=1,
reasoning=226, cache_read=1856): our pricing reproduces opencode's stored USD
cost ($0.0069452) exactly. Tests anchored to that real row.
This commit is contained in:
Renn F
2026-06-18 09:57:20 +02:00
parent af6cad98d8
commit b0857915a1
7 changed files with 52 additions and 10 deletions
+2 -1
View File
@@ -71,7 +71,8 @@ def test_build_opencode_config_provider_and_model() -> None:
instruction_paths=["/app/system-prompt.md"],
)
provider = cfg["provider"]["xai"]
assert provider["npm"] == "@ai-sdk/openai-compatible"
# grok-build-0.1 needs the Responses API → @ai-sdk/openai, not -compatible.
assert provider["npm"] == "@ai-sdk/openai"
assert provider["options"]["baseURL"] == "https://api.x.ai/v1"
assert provider["options"]["apiKey"] == "xai-key"
assert "grok-build-0.1" in provider["models"]
+23
View File
@@ -28,6 +28,11 @@ _S2_IN, _S2_OUT, _S2_CREAD = 200, 70, 10
# grok-build-0.1: 1M input ($1.00) + 1M output ($2.00) = $3.00.
_GROK_COST_1M_1M = 3.00
# A REAL grok-build-0.1 session row observed from a live opencode run. Our
# pricing must reproduce opencode's own stored `cost` (= xAI authoritative).
_REAL_IN, _REAL_OUT, _REAL_REASON, _REAL_CREAD = 6120, 1, 226, 1856
_REAL_COST = 0.0069452
def _make_db(
path: Path, rows: list[tuple[str, int, int, int, int, int, float]]
@@ -109,3 +114,21 @@ def test_cost_for_session_missing_db(tmp_path: Path) -> None:
usage, cost = cost_for_session("grok-build-0.1", tmp_path / "nope.db")
assert usage is None
assert cost == _ZERO_COST
def test_cost_reproduces_opencode_authoritative_cost(tmp_path: Path) -> None:
"""Real observed row: our pricing must match opencode's stored USD cost.
Proves the column semantics (non-cached input disjoint from cache_read;
reasoning separate, billed at output rate).
"""
db = tmp_path / "opencode.db"
# (id, input, output, reasoning, cache_read, cache_write, cost)
_make_db(
db,
[("real", _REAL_IN, _REAL_OUT, _REAL_REASON, _REAL_CREAD, 0, _REAL_COST)],
)
usage, cost = cost_for_session("grok-build-0.1", db, session_id="real")
assert usage is not None
assert abs(cost - _REAL_COST) < _TOL
assert abs(cost - usage.opencode_cost) < _TOL