roadmodel 0.2.55__tar.gz → 0.2.56__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {roadmodel-0.2.55 → roadmodel-0.2.56}/PKG-INFO +1 -1
- {roadmodel-0.2.55 → roadmodel-0.2.56}/pyproject.toml +1 -1
- {roadmodel-0.2.55 → roadmodel-0.2.56}/src/roadmodel/__init__.py +1 -1
- roadmodel-0.2.56/src/roadmodel/providers/anthropic.py +138 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/src/roadmodel/providers/google.py +48 -4
- {roadmodel-0.2.55 → roadmodel-0.2.56}/src/roadmodel/providers/openai.py +31 -5
- {roadmodel-0.2.55 → roadmodel-0.2.56}/src/roadmodel/providers/openai_compatible.py +17 -0
- roadmodel-0.2.56/src/roadmodel/usage.py +90 -0
- roadmodel-0.2.55/src/roadmodel/providers/anthropic.py +0 -57
- {roadmodel-0.2.55 → roadmodel-0.2.56}/.gitignore +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/LICENSE +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/NOTICE +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/README.md +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/docs/benchmarks.json +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/docs/catalog.json +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/docs/model-selector.txt +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/docs/model-tier-cost-scale.md +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/docs/settings-display.md +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/docs/templates/phase-roadmap-template.md +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/docs/templates/planning-kit-how-to-use.md +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/docs/templates/project-roadmap-template.md +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/docs/templates/prompt-phase-roadmap.md +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/docs/templates/prompt-project-roadmap.md +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/docs/user-context.example.md +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/hatch_build.py +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/infra/README.md +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/infra/gcp-killswitch/README.md +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/infra/supabase/README.md +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/roadmodel/data/benchmarks.json +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/roadmodel/data/catalog.json +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/roadmodel/data/model-selector.txt +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/roadmodel/data/model-tier-cost-scale.md +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/roadmodel/data/phase-roadmap-template.md +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/roadmodel/data/planning-kit-how-to-use.md +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/roadmodel/data/project-roadmap-template.md +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/roadmodel/data/prompt-phase-roadmap.md +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/roadmodel/data/prompt-project-roadmap.md +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/roadmodel/data/settings-display.md +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/roadmodel/data/user-context.example.md +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/scripts/update_projects.py +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/src/roadmodel/__main__.py +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/src/roadmodel/cli.py +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/src/roadmodel/config.py +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/src/roadmodel/cost.py +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/src/roadmodel/errors.py +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/src/roadmodel/mcp_server.py +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/src/roadmodel/providers/__init__.py +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/src/roadmodel/providers/registry.py +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/src/roadmodel/recommend.py +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/src/roadmodel/scoring.py +0 -0
- {roadmodel-0.2.55 → roadmodel-0.2.56}/src/roadmodel/user_context.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: roadmodel
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.56
|
|
4
4
|
Summary: BYO-key CLI that recommends the right AI model, platform, and settings for a prompt.
|
|
5
5
|
Project-URL: Homepage, https://roadmodel.ai
|
|
6
6
|
Project-URL: Repository, https://github.com/nathanramoscfa/roadmodel
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
# src/roadmodel/__init__.py
|
|
2
|
-
__version__ = "0.2.
|
|
2
|
+
__version__ = "0.2.56"
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# src/roadmodel/providers/anthropic.py
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import re
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from roadmodel import usage
|
|
8
|
+
from roadmodel.errors import ProviderCallError
|
|
9
|
+
|
|
10
|
+
DEFAULT_MODEL = "claude-sonnet-4-6"
|
|
11
|
+
|
|
12
|
+
# claude-<family>-<major>[-<minor>][-<yyyymmdd>]: claude-haiku-4-5-20251001,
|
|
13
|
+
# claude-sonnet-5, claude-opus-5-5, claude-fable-5-1.
|
|
14
|
+
_MODEL_RE = re.compile(
|
|
15
|
+
r"^claude-(?P<family>[a-z]+)-(?P<major>\d+)(?:-(?P<minor>\d{1,2}))?(?:-\d{8})?$"
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _generation(model_id: str) -> tuple[str, tuple[int, int]] | None:
|
|
20
|
+
match = _MODEL_RE.match(model_id)
|
|
21
|
+
if match is None:
|
|
22
|
+
return None
|
|
23
|
+
return match["family"], (int(match["major"]), int(match["minor"] or 0))
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _takes_sampling(model_id: str) -> bool:
|
|
27
|
+
"""Whether the model still accepts `temperature`. Sonnet 5, Opus 4.7 and
|
|
28
|
+
later, and the Fable/Mythos line reject sampling parameters with a 400; an
|
|
29
|
+
id this cannot read is sent without it, the safe side of that 400."""
|
|
30
|
+
gen = _generation(model_id)
|
|
31
|
+
if gen is None:
|
|
32
|
+
return False
|
|
33
|
+
family, version = gen
|
|
34
|
+
if family == "haiku":
|
|
35
|
+
return True
|
|
36
|
+
if family == "sonnet":
|
|
37
|
+
return version < (5, 0)
|
|
38
|
+
if family == "opus":
|
|
39
|
+
return version < (4, 7)
|
|
40
|
+
return False
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _takes_effort(model_id: str) -> bool:
|
|
44
|
+
"""Whether the model accepts output_config.effort: Opus 4.5 and later,
|
|
45
|
+
Sonnet 4.6 and later, and the Fable/Mythos line. Haiku 4.5 rejects it."""
|
|
46
|
+
gen = _generation(model_id)
|
|
47
|
+
if gen is None:
|
|
48
|
+
return False
|
|
49
|
+
family, version = gen
|
|
50
|
+
if family == "opus":
|
|
51
|
+
return version >= (4, 5)
|
|
52
|
+
if family == "sonnet":
|
|
53
|
+
return version >= (4, 6)
|
|
54
|
+
return family in ("fable", "mythos")
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _record_usage(model_id: str, response: object) -> None:
|
|
58
|
+
"""Report the Messages API's token counts to roadmodel.usage. Anthropic
|
|
59
|
+
splits the prompt into uncached input, cache reads and cache writes; the
|
|
60
|
+
total is their sum."""
|
|
61
|
+
u = getattr(response, "usage", None)
|
|
62
|
+
if u is None:
|
|
63
|
+
return
|
|
64
|
+
uncached = getattr(u, "input_tokens", None)
|
|
65
|
+
read = getattr(u, "cache_read_input_tokens", None) or 0
|
|
66
|
+
write = getattr(u, "cache_creation_input_tokens", None) or 0
|
|
67
|
+
total = uncached + read + write if isinstance(uncached, int) else None
|
|
68
|
+
usage.record(
|
|
69
|
+
"anthropic",
|
|
70
|
+
model_id,
|
|
71
|
+
input_tokens=total,
|
|
72
|
+
output_tokens=getattr(u, "output_tokens", None),
|
|
73
|
+
cached_input_tokens=read,
|
|
74
|
+
cache_write_tokens=write,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def recommend(
|
|
79
|
+
prompt: str,
|
|
80
|
+
system: str,
|
|
81
|
+
*,
|
|
82
|
+
model: str | None = None,
|
|
83
|
+
api_key: str,
|
|
84
|
+
max_output_tokens: int | None = None,
|
|
85
|
+
thinking_budget: int | None = None,
|
|
86
|
+
temperature: float | None = None,
|
|
87
|
+
) -> str:
|
|
88
|
+
try:
|
|
89
|
+
from anthropic import Anthropic, APIError
|
|
90
|
+
except Exception as exc: # pragma: no cover - dependency/runtime guard
|
|
91
|
+
raise ProviderCallError(
|
|
92
|
+
"The Anthropic SDK is required for `roadmodel recommend` but is not "
|
|
93
|
+
"installed. Install the engine extra: pip install 'roadmodel[recommend]'."
|
|
94
|
+
) from exc
|
|
95
|
+
|
|
96
|
+
model_id = model or DEFAULT_MODEL
|
|
97
|
+
# `kwargs` is typed `Any` for the same reason as providers/openai.py: the
|
|
98
|
+
# SDK's overloaded `create` signature rejects a mixed-value dict literal.
|
|
99
|
+
kwargs: Any = {
|
|
100
|
+
"model": model_id,
|
|
101
|
+
"max_tokens": max_output_tokens if max_output_tokens is not None else 4096,
|
|
102
|
+
# The system prompt is the selector + catalog (~60k tokens) and is the
|
|
103
|
+
# same on every call, so it is cached: a warm call reads it at a tenth
|
|
104
|
+
# of the input price instead of paying for it again.
|
|
105
|
+
"system": [{"type": "text", "text": system, "cache_control": {"type": "ephemeral"}}],
|
|
106
|
+
"messages": [{"role": "user", "content": prompt}],
|
|
107
|
+
}
|
|
108
|
+
# thinking_budget is the Protocol's "how hard to reason" dial. Current Claude
|
|
109
|
+
# models take no token budget (it is a 400 on Sonnet 5 and Opus 4.7+), so
|
|
110
|
+
# any value maps to the lowest effort, which keeps the classification fast,
|
|
111
|
+
# like reasoning.effort on providers/openai.py. Thinking itself is left at
|
|
112
|
+
# the model's default: Opus 5.5 cannot disable it, and lowering effort is
|
|
113
|
+
# the documented way to make it brief.
|
|
114
|
+
if thinking_budget is not None and _takes_effort(model_id):
|
|
115
|
+
kwargs["output_config"] = {"effort": "low"}
|
|
116
|
+
# Recommender determinism (#176), on the models that still take sampling.
|
|
117
|
+
if temperature is not None and _takes_sampling(model_id):
|
|
118
|
+
kwargs["temperature"] = temperature
|
|
119
|
+
|
|
120
|
+
try:
|
|
121
|
+
client = Anthropic(api_key=api_key)
|
|
122
|
+
response = client.messages.create(**kwargs)
|
|
123
|
+
_record_usage(model_id, response)
|
|
124
|
+
text_parts: list[str] = []
|
|
125
|
+
for block in getattr(response, "content", []) or []:
|
|
126
|
+
if getattr(block, "type", None) == "text":
|
|
127
|
+
piece = getattr(block, "text", "")
|
|
128
|
+
if piece:
|
|
129
|
+
text_parts.append(piece)
|
|
130
|
+
if text_parts:
|
|
131
|
+
return "".join(text_parts).strip()
|
|
132
|
+
raise ProviderCallError("Anthropic response did not contain text output.")
|
|
133
|
+
except ProviderCallError:
|
|
134
|
+
raise
|
|
135
|
+
except APIError as exc:
|
|
136
|
+
raise ProviderCallError(f"Anthropic API call failed: {exc}") from exc
|
|
137
|
+
except Exception as exc: # pragma: no cover - defensive adapter guard
|
|
138
|
+
raise ProviderCallError(f"Anthropic API call failed ({type(exc).__name__}).") from exc
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
# src/roadmodel/providers/google.py
|
|
2
2
|
from __future__ import annotations
|
|
3
3
|
|
|
4
|
+
import re
|
|
4
5
|
from typing import Any
|
|
5
6
|
|
|
7
|
+
from roadmodel import usage
|
|
6
8
|
from roadmodel.errors import ProviderCallError
|
|
7
9
|
|
|
8
10
|
# Default engine model for direct Google API calls when no model is passed.
|
|
@@ -14,6 +16,45 @@ from roadmodel.errors import ProviderCallError
|
|
|
14
16
|
# Prod (the hosted service) forces an explicit model and never relies on this.
|
|
15
17
|
DEFAULT_MODEL = "gemini-2.5-pro"
|
|
16
18
|
|
|
19
|
+
# The 2.x generation takes a numeric thinking_budget; from Gemini 3 on, the API
|
|
20
|
+
# documents discrete thinking LEVELS instead (update/gemini-thinking.json), so a
|
|
21
|
+
# numeric budget is not the dial those models expose.
|
|
22
|
+
_BUDGET_GENERATION_RE = re.compile(r"^gemini-[12](?:\.|-)")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _thinking_config(model_id: str, thinking_budget: int) -> dict[str, Any]:
|
|
26
|
+
"""The thinking dial for this model: the numeric budget on the 2.x models,
|
|
27
|
+
a level on Gemini 3 and later. `low` is the floor every Gemini 3 model
|
|
28
|
+
accepts (3.8 Flash, 3.7 Flash and 3.1 Pro have no `minimal`), so a budget of
|
|
29
|
+
0 (the recommender's "as fast as possible") maps to it; larger budgets map to
|
|
30
|
+
the level whose spend they are closest to."""
|
|
31
|
+
if _BUDGET_GENERATION_RE.match(model_id):
|
|
32
|
+
return {"thinking_budget": thinking_budget}
|
|
33
|
+
if thinking_budget <= 1024:
|
|
34
|
+
return {"thinking_level": "low"}
|
|
35
|
+
if thinking_budget <= 8192:
|
|
36
|
+
return {"thinking_level": "medium"}
|
|
37
|
+
return {"thinking_level": "high"}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _record_usage(model_id: str, response: object) -> None:
|
|
41
|
+
"""Report Gemini's usage_metadata to roadmodel.usage. Thought tokens are
|
|
42
|
+
billed at the output rate, so they count as output."""
|
|
43
|
+
meta = getattr(response, "usage_metadata", None)
|
|
44
|
+
if meta is None:
|
|
45
|
+
return
|
|
46
|
+
candidates = getattr(meta, "candidates_token_count", None)
|
|
47
|
+
thoughts = getattr(meta, "thoughts_token_count", None)
|
|
48
|
+
output = (candidates or 0) + (thoughts or 0) if isinstance(candidates, int) else None
|
|
49
|
+
usage.record(
|
|
50
|
+
"google",
|
|
51
|
+
model_id,
|
|
52
|
+
input_tokens=getattr(meta, "prompt_token_count", None),
|
|
53
|
+
output_tokens=output,
|
|
54
|
+
cached_input_tokens=getattr(meta, "cached_content_token_count", None) or 0,
|
|
55
|
+
reasoning_tokens=thoughts,
|
|
56
|
+
)
|
|
57
|
+
|
|
17
58
|
|
|
18
59
|
def recommend(
|
|
19
60
|
prompt: str,
|
|
@@ -42,13 +83,15 @@ def recommend(
|
|
|
42
83
|
config: Any = {"system_instruction": system}
|
|
43
84
|
if max_output_tokens is not None:
|
|
44
85
|
config["max_output_tokens"] = max_output_tokens
|
|
86
|
+
model_id = model or DEFAULT_MODEL
|
|
45
87
|
if thinking_budget is not None:
|
|
46
88
|
# Gemini 2.5+ Flash reasons by default, and that reasoning is
|
|
47
89
|
# decoded before the visible answer (and counts against
|
|
48
90
|
# max_output_tokens). thinking_budget caps it: 0 disables
|
|
49
|
-
# thinking entirely, a small value bounds it
|
|
50
|
-
#
|
|
51
|
-
|
|
91
|
+
# thinking entirely on 2.5, a small value bounds it, and on
|
|
92
|
+
# Gemini 3+ it selects the matching level (_thinking_config).
|
|
93
|
+
# `is not None` — not truthiness — because 0 is a meaningful value.
|
|
94
|
+
config["thinking_config"] = _thinking_config(model_id, thinking_budget)
|
|
52
95
|
if temperature is not None:
|
|
53
96
|
# Recommender determinism (#176): without this Gemini samples at
|
|
54
97
|
# its default temperature (~1.0), so identical input yields
|
|
@@ -56,10 +99,11 @@ def recommend(
|
|
|
56
99
|
# — because 0.0 (greedy/deterministic) is the intended value.
|
|
57
100
|
config["temperature"] = temperature
|
|
58
101
|
response = client.models.generate_content(
|
|
59
|
-
model=
|
|
102
|
+
model=model_id,
|
|
60
103
|
contents=prompt,
|
|
61
104
|
config=config,
|
|
62
105
|
)
|
|
106
|
+
_record_usage(model_id, response)
|
|
63
107
|
text = getattr(response, "text", None)
|
|
64
108
|
if isinstance(text, str) and text.strip():
|
|
65
109
|
return text.strip()
|
|
@@ -4,15 +4,40 @@ from __future__ import annotations
|
|
|
4
4
|
import re
|
|
5
5
|
from typing import Any
|
|
6
6
|
|
|
7
|
+
from roadmodel import usage
|
|
7
8
|
from roadmodel.errors import ProviderCallError
|
|
8
9
|
|
|
9
10
|
DEFAULT_MODEL = "gpt-5.4"
|
|
10
11
|
|
|
11
12
|
|
|
13
|
+
# The reasoning generations: gpt-5 and everything after it (gpt-6, gpt-6.1,
|
|
14
|
+
# ...). Each one bills reasoning against max_output_tokens, so each gets the
|
|
15
|
+
# reasoning.effort cap below. Matching only "gpt-5" left the gpt-6 line running
|
|
16
|
+
# at its default effort: slower, dearer, and free to spend the whole output
|
|
17
|
+
# budget thinking.
|
|
18
|
+
_REASONING_RE = re.compile(r"^gpt-(?:5|[6-9])")
|
|
19
|
+
|
|
12
20
|
# gpt-5.6 and later dropped the `minimal` reasoning rung; their floor is `low`.
|
|
13
21
|
_NO_MINIMAL_RE = re.compile(r"^gpt-(?:5\.[6-9]|5\.\d\d|[6-9])")
|
|
14
22
|
|
|
15
23
|
|
|
24
|
+
def _record_usage(model_id: str, response: object) -> None:
|
|
25
|
+
"""Report the Responses API's token counts to roadmodel.usage."""
|
|
26
|
+
u = getattr(response, "usage", None)
|
|
27
|
+
if u is None:
|
|
28
|
+
return
|
|
29
|
+
usage.record(
|
|
30
|
+
"openai",
|
|
31
|
+
model_id,
|
|
32
|
+
input_tokens=getattr(u, "input_tokens", None),
|
|
33
|
+
output_tokens=getattr(u, "output_tokens", None),
|
|
34
|
+
cached_input_tokens=getattr(getattr(u, "input_tokens_details", None), "cached_tokens", 0),
|
|
35
|
+
reasoning_tokens=getattr(
|
|
36
|
+
getattr(u, "output_tokens_details", None), "reasoning_tokens", None
|
|
37
|
+
),
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
|
|
16
41
|
def _extract_output_text(response: object) -> str | None:
|
|
17
42
|
output_text = getattr(response, "output_text", None)
|
|
18
43
|
if isinstance(output_text, str) and output_text.strip():
|
|
@@ -71,16 +96,17 @@ def recommend(
|
|
|
71
96
|
}
|
|
72
97
|
if max_output_tokens is not None:
|
|
73
98
|
kwargs["max_output_tokens"] = max_output_tokens
|
|
74
|
-
# Cap reasoning on gpt-5
|
|
75
|
-
# max_output_tokens budget (see the note above). Default low; a
|
|
99
|
+
# Cap reasoning on the gpt-5+ reasoning models so it doesn't eat the
|
|
100
|
+
# whole max_output_tokens budget (see the note above). Default low; a
|
|
76
101
|
# thinking_budget of 0 selects the FLOOR of that model's ladder for the
|
|
77
|
-
# anon tier — `minimal` on the gpt-5.0-5.5 generation, `low`
|
|
78
|
-
# gpt-5.6 generation, which rejects `minimal` outright ("Unsupported
|
|
102
|
+
# anon tier — `minimal` on the gpt-5.0-5.5 generation, `low` from the
|
|
103
|
+
# gpt-5.6 generation on, which rejects `minimal` outright ("Unsupported
|
|
79
104
|
# value: 'minimal' is not supported with the 'gpt-5.6-luna' model").
|
|
80
|
-
if
|
|
105
|
+
if _REASONING_RE.match(model_id):
|
|
81
106
|
floor = "low" if _NO_MINIMAL_RE.match(model_id) else "minimal"
|
|
82
107
|
kwargs["reasoning"] = {"effort": floor if thinking_budget == 0 else "low"}
|
|
83
108
|
response = client.responses.create(**kwargs)
|
|
109
|
+
_record_usage(model_id, response)
|
|
84
110
|
text = _extract_output_text(response)
|
|
85
111
|
if text:
|
|
86
112
|
return text
|
|
@@ -19,6 +19,7 @@ from __future__ import annotations
|
|
|
19
19
|
import re
|
|
20
20
|
from typing import Any
|
|
21
21
|
|
|
22
|
+
from roadmodel import usage
|
|
22
23
|
from roadmodel.errors import ProviderCallError
|
|
23
24
|
from roadmodel.providers.registry import (
|
|
24
25
|
COMPATIBLE_PROVIDERS,
|
|
@@ -54,6 +55,21 @@ def _extract_message_text(response: object) -> str | None:
|
|
|
54
55
|
return None
|
|
55
56
|
|
|
56
57
|
|
|
58
|
+
def _record_usage(provider: str, model_id: str, response: object) -> None:
|
|
59
|
+
"""Report Chat Completions token counts to roadmodel.usage."""
|
|
60
|
+
u = getattr(response, "usage", None)
|
|
61
|
+
if u is None:
|
|
62
|
+
return
|
|
63
|
+
details = getattr(u, "prompt_tokens_details", None)
|
|
64
|
+
usage.record(
|
|
65
|
+
provider,
|
|
66
|
+
model_id,
|
|
67
|
+
input_tokens=getattr(u, "prompt_tokens", None),
|
|
68
|
+
output_tokens=getattr(u, "completion_tokens", None),
|
|
69
|
+
cached_input_tokens=getattr(details, "cached_tokens", None) or 0,
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
|
|
57
73
|
def _reasoning_only(response: object) -> bool:
|
|
58
74
|
choices = getattr(response, "choices", None) or []
|
|
59
75
|
if not choices:
|
|
@@ -161,6 +177,7 @@ class OpenAICompatibleAdapter:
|
|
|
161
177
|
kwargs["temperature"] = temperature
|
|
162
178
|
kwargs.update(_reasoning_kwargs(spec, thinking_budget))
|
|
163
179
|
response = client.chat.completions.create(**kwargs)
|
|
180
|
+
_record_usage(spec.name, model_id, response)
|
|
164
181
|
text = _extract_message_text(response)
|
|
165
182
|
if text:
|
|
166
183
|
return text
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# src/roadmodel/usage.py
|
|
2
|
+
"""What the last engine call actually consumed, as the provider reported it.
|
|
3
|
+
|
|
4
|
+
The recommender's cost has always been an ESTIMATE (a fixed prompt size times a
|
|
5
|
+
rate card), and the estimate drifts: the static prompt grew from ~20k to ~61k
|
|
6
|
+
tokens while the estimate stood still, and prompt caching makes the real bill a
|
|
7
|
+
fraction of the list price on a warm prefix. Each provider adapter records the
|
|
8
|
+
token counts its SDK returned here, so a caller (the hosted service) can meter
|
|
9
|
+
the call exactly instead of guessing.
|
|
10
|
+
|
|
11
|
+
The record lives in a :class:`~contextvars.ContextVar`, so concurrent requests
|
|
12
|
+
(threads, asyncio tasks) never read each other's numbers, and the
|
|
13
|
+
``ProviderAdapter`` protocol keeps returning plain text. Recording is
|
|
14
|
+
best-effort by construction: an SDK response without a usage block records
|
|
15
|
+
nothing, and nothing here can raise into a recommendation.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
from contextvars import ContextVar
|
|
21
|
+
from dataclasses import asdict, dataclass
|
|
22
|
+
from typing import Any
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass(frozen=True)
|
|
26
|
+
class CallUsage:
|
|
27
|
+
provider: str
|
|
28
|
+
model: str
|
|
29
|
+
# Every prompt token the call sent, cached or not.
|
|
30
|
+
input_tokens: int
|
|
31
|
+
# The part of input_tokens served from the provider's prompt cache (billed
|
|
32
|
+
# at the cache-read rate).
|
|
33
|
+
cached_input_tokens: int
|
|
34
|
+
# The part of input_tokens written to the cache on this call, for providers
|
|
35
|
+
# that bill a write separately (Anthropic); 0 elsewhere.
|
|
36
|
+
cache_write_tokens: int
|
|
37
|
+
# Every generated token, reasoning included (all providers bill reasoning
|
|
38
|
+
# at the output rate).
|
|
39
|
+
output_tokens: int
|
|
40
|
+
# The reasoning share of output_tokens, when the provider reports it.
|
|
41
|
+
reasoning_tokens: int | None = None
|
|
42
|
+
|
|
43
|
+
def as_dict(self) -> dict[str, Any]:
|
|
44
|
+
return asdict(self)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
_LAST: ContextVar[CallUsage | None] = ContextVar("roadmodel_last_usage", default=None)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def reset() -> None:
|
|
51
|
+
"""Forget the previous call's usage, before making a new one."""
|
|
52
|
+
_LAST.set(None)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def last() -> CallUsage | None:
|
|
56
|
+
"""The usage the most recent engine call in this context recorded, if any."""
|
|
57
|
+
return _LAST.get()
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _int(value: object) -> int | None:
|
|
61
|
+
return value if isinstance(value, int) and not isinstance(value, bool) else None
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def record(
|
|
65
|
+
provider: str,
|
|
66
|
+
model: str,
|
|
67
|
+
*,
|
|
68
|
+
input_tokens: object,
|
|
69
|
+
output_tokens: object,
|
|
70
|
+
cached_input_tokens: object = 0,
|
|
71
|
+
cache_write_tokens: object = 0,
|
|
72
|
+
reasoning_tokens: object = None,
|
|
73
|
+
) -> None:
|
|
74
|
+
"""Store one call's counts. Silently skips a response whose totals are not
|
|
75
|
+
integers (an SDK that omits usage), so a missing block never fails a call."""
|
|
76
|
+
total_in = _int(input_tokens)
|
|
77
|
+
total_out = _int(output_tokens)
|
|
78
|
+
if total_in is None or total_out is None:
|
|
79
|
+
return
|
|
80
|
+
_LAST.set(
|
|
81
|
+
CallUsage(
|
|
82
|
+
provider=provider,
|
|
83
|
+
model=model,
|
|
84
|
+
input_tokens=total_in,
|
|
85
|
+
cached_input_tokens=_int(cached_input_tokens) or 0,
|
|
86
|
+
cache_write_tokens=_int(cache_write_tokens) or 0,
|
|
87
|
+
output_tokens=total_out,
|
|
88
|
+
reasoning_tokens=_int(reasoning_tokens),
|
|
89
|
+
)
|
|
90
|
+
)
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
# src/roadmodel/providers/anthropic.py
|
|
2
|
-
from __future__ import annotations
|
|
3
|
-
|
|
4
|
-
from roadmodel.errors import ProviderCallError
|
|
5
|
-
|
|
6
|
-
DEFAULT_MODEL = "claude-sonnet-4-6"
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
def recommend(
|
|
10
|
-
prompt: str,
|
|
11
|
-
system: str,
|
|
12
|
-
*,
|
|
13
|
-
model: str | None = None,
|
|
14
|
-
api_key: str,
|
|
15
|
-
max_output_tokens: int | None = None,
|
|
16
|
-
thinking_budget: int | None = None,
|
|
17
|
-
temperature: float | None = None,
|
|
18
|
-
) -> str:
|
|
19
|
-
# thinking_budget and temperature are accepted for ProviderAdapter Protocol
|
|
20
|
-
# parity but intentionally NOT forwarded: both are Gemini-specific knobs for
|
|
21
|
-
# the recommender latency/determinism work (issues #132, #176). Anthropic
|
|
22
|
-
# extended-thinking has different semantics (a `thinking` block with its own
|
|
23
|
-
# budget_tokens and minimums) and the recommender response shape does not
|
|
24
|
-
# tolerate small caps on Anthropic at all (PR #128). Anthropic reasoning
|
|
25
|
-
# control is Phase 5 paid-frontier scope.
|
|
26
|
-
_ = (thinking_budget, temperature)
|
|
27
|
-
try:
|
|
28
|
-
from anthropic import Anthropic, APIError
|
|
29
|
-
except Exception as exc: # pragma: no cover - dependency/runtime guard
|
|
30
|
-
raise ProviderCallError(
|
|
31
|
-
"The Anthropic SDK is required for `roadmodel recommend` but is not "
|
|
32
|
-
"installed. Install the engine extra: pip install 'roadmodel[recommend]'."
|
|
33
|
-
) from exc
|
|
34
|
-
|
|
35
|
-
try:
|
|
36
|
-
client = Anthropic(api_key=api_key)
|
|
37
|
-
response = client.messages.create(
|
|
38
|
-
model=model or DEFAULT_MODEL,
|
|
39
|
-
max_tokens=max_output_tokens if max_output_tokens is not None else 4096,
|
|
40
|
-
system=system,
|
|
41
|
-
messages=[{"role": "user", "content": prompt}],
|
|
42
|
-
)
|
|
43
|
-
text_parts: list[str] = []
|
|
44
|
-
for block in getattr(response, "content", []) or []:
|
|
45
|
-
if getattr(block, "type", None) == "text":
|
|
46
|
-
piece = getattr(block, "text", "")
|
|
47
|
-
if piece:
|
|
48
|
-
text_parts.append(piece)
|
|
49
|
-
if text_parts:
|
|
50
|
-
return "".join(text_parts).strip()
|
|
51
|
-
raise ProviderCallError("Anthropic response did not contain text output.")
|
|
52
|
-
except ProviderCallError:
|
|
53
|
-
raise
|
|
54
|
-
except APIError as exc:
|
|
55
|
-
raise ProviderCallError(f"Anthropic API call failed: {exc}") from exc
|
|
56
|
-
except Exception as exc: # pragma: no cover - defensive adapter guard
|
|
57
|
-
raise ProviderCallError(f"Anthropic API call failed ({type(exc).__name__}).") from exc
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|