codenib.llm
¶
LLM module for CodeNib.
Modules:
| Name | Description |
|---|---|
decisions |
Typed decisions over OpenRouter's Decisions API, separate from chat completion. |
diagnostics |
Static diagnostics for LiteLLM-backed product configuration. |
litellm_chat |
Lightweight LLM chat wrapper around LiteLLM. |
options |
Validated LiteLLM request options shared by CLI and web runtimes. |
usage |
Token usage and cost tracking for LLM calls. |
Classes:
| Name | Description |
|---|---|
ChoiceAnswer |
Selected alternative, its full distribution, and model confidence. |
ChoiceQuestion |
Choose among named alternatives described by their criteria. |
DecisionResult |
Answers and provider metadata, including actual token usage and cost. |
NoulAnswer |
The probability of yes; workflow thresholds remain the caller's policy. |
NoulQuestion |
Ask for the probability that a condition holds, without choosing a threshold. |
OpenRouterDecisions |
Small synchronous client for Jev's |
ScoreAnswer |
Expected position on the question's scale, with its full distribution. |
ScoreQuestion |
Score on an ordered scale from zero to |
ChatMessage |
A single chat message with role and content. |
LiteLLMChat |
Thin wrapper around |
RetryConfig |
Exponential-backoff retry policy for transient LLM errors. |
ChoiceAnswer
¶
Bases: BaseModel
Selected alternative, its full distribution, and model confidence.
ChoiceQuestion
¶
Bases: _Question
Choose among named alternatives described by their criteria.
DecisionResult
¶
Bases: BaseModel
Answers and provider metadata, including actual token usage and cost.
Methods:
| Name | Description |
|---|---|
validate_questions |
Reject mismatched answers before they can influence application policy. |
validate_questions
¶
Reject mismatched answers before they can influence application policy.
Source code in codenib/llm/decisions.py
NoulAnswer
¶
Bases: BaseModel
The probability of yes; workflow thresholds remain the caller's policy.
NoulQuestion
¶
Bases: _Question
Ask for the probability that a condition holds, without choosing a threshold.
OpenRouterDecisions
dataclass
¶
OpenRouterDecisions(
model: str = "~typesafe/jev-latest",
api_key: str | None = None,
timeout: float = 30.0,
max_retries: int = 2,
trust_env: bool = True,
)
Small synchronous client for Jev's state + questions -> answers API.
Reads OPENROUTER_API_KEY at invocation time unless api_key is supplied.
Only transport failures, HTTP 408/429, and HTTP 5xx are retried. This client
has no chat, tool-calling, or text-generation interface.
Methods:
| Name | Description |
|---|---|
decide |
Evaluate a batch of typed questions against the same JSON-compatible state. |
decide
¶
decide(
*, state: Any, questions: Mapping[str, DecisionQuestion | dict[str, Any]]
) -> DecisionResult
Evaluate a batch of typed questions against the same JSON-compatible state.
Source code in codenib/llm/decisions.py
ScoreAnswer
¶
Bases: BaseModel
Expected position on the question's scale, with its full distribution.
ScoreQuestion
¶
Bases: _Question
Score on an ordered scale from zero to len(criteria) - 1.
ChatMessage
dataclass
¶
A single chat message with role and content.
LiteLLMChat
dataclass
¶
LiteLLMChat(
model: str,
temperature: float | None = 0.0,
max_tokens: int | None = 8192,
api_key: str | None = None,
api_base: str | None = None,
extra_kwargs: dict[str, Any] = dict(),
retry: RetryConfig = RetryConfig(),
)
Thin wrapper around litellm.completion.
Provides .invoke() and .with_structured_output() matching the
interface that callers already use via LangChain chat models.
Methods:
| Name | Description |
|---|---|
invoke |
Send messages and return the assistant content string. |
complete |
Complete raw LiteLLM message dictionaries and return text content. |
with_structured_output |
Return a callable that parses LLM responses into schema. |
Attributes:
| Name | Type | Description |
|---|---|---|
cache_identity |
str
|
Stable non-secret identity for model-dependent artifact caches. |
cache_identity
property
¶
Stable non-secret identity for model-dependent artifact caches.
invoke
¶
invoke(messages: list[ChatMessage]) -> str
Send messages and return the assistant content string.
complete
¶
Complete raw LiteLLM message dictionaries and return text content.
Source code in codenib/llm/litellm_chat.py
with_structured_output
¶
Return a callable that parses LLM responses into schema.
RetryConfig
dataclass
¶
RetryConfig(
max_retries: int = 2,
base_delay: float = 0.5,
max_delay: float = 8.0,
jitter: float = 0.1,
)
Exponential-backoff retry policy for transient LLM errors.
max_retries is the number of additional attempts after the first,
so the call is made at most max_retries + 1 times. base_delay is
the first backoff in seconds; each subsequent wait is base_delay *
2**attempt capped at max_delay. A small random jitter spreads
retries out so concurrent callers don't synchronize.
Methods:
| Name | Description |
|---|---|
backoff |
Seconds to sleep before retry |
backoff
¶
Seconds to sleep before retry attempt (0-based).