Skip to content

LLM providers

limen_agents.llm.resolver

Which provider to use, from a plain :class:LlmConfig.

The order, when provider is not given:

  1. an Anthropic key, if the SDK is installed;
  2. an OpenAI key, if the SDK is installed;
  3. a Foundry endpoint and key;
  4. llama.cpp, the self-hosted server: the fallback with no credentials.

A cloud key whose SDK is missing does not crash: the self-hosted engine is the intended one where the SDKs are not installed. provider set explicitly wins, and fails loudly when its credentials are missing.

LlmConfig dataclass

Everything the resolver needs, with no settings framework behind it.

role_models maps a role ("RiskAnalyst") to a model id for the cloud providers. declared_role_models is the part of it the operator really set: the self-hosted engines use only that, because default Claude ids mean nothing to a local gateway, and every other role falls back to the engine's *_model.

sdk_available

sdk_available(module: str) -> bool

Whether module can be imported.

build

build(config: LlmConfig, provider: Provider, *, budget: Budget = NO_BUDGET, post: Post = post_with_retry) -> LlmClientFactory

The factory of provider, without checking credentials.

resolve

resolve(config: LlmConfig, *, budget: Budget = NO_BUDGET, post: Post = post_with_retry) -> LlmClientFactory

The factory the configuration selects, in the order of the module docstring.

limen_agents.llm.base

Vendor-agnostic chat-client Protocols.

The intent is minimal: every executor + agent code path only depends on :class:ChatClient. Provider SDKs (Anthropic, OpenAI, …) live behind concrete factories that lazily import their dependencies.

ChatMessage dataclass

One chat turn — the same DTO whatever the backing provider.

LlmFactoryError

Bases: RuntimeError

Raised when a factory can't be constructed (missing SDK, missing creds).

ChatClient

Bases: Protocol

A thin async chat-completion abstraction.

Implementations must:

  • Be deterministic given the same inputs and a temperature of 0 (best-effort — vendors may drift; we don't depend on it).
  • Return raw text. JSON / Pydantic schemas live one layer up in the ChatAgent's response-validation logic.

model property

model: str

The concrete model id this client targets.

chat async

chat(messages: Sequence[ChatMessage], *, temperature: float = 0.0, max_tokens: int | None = None, response_format: Literal['text', 'json_object'] = 'text') -> str

Return the assistant message content for messages.

LlmClientFactory

Bases: Protocol

Builds a :class:ChatClient for a given agent role.

provider property

provider: str

A short label for the provider ("anthropic", "openai"…).

create

create(agent_role: str) -> ChatClient

Construct the client for agent_role ("RiskAnalyst" etc.).

limen_agents.llm.anthropic

Claude through the official anthropic SDK (extra providers).

AnthropicChatClient dataclass

A :class:~limen_agents.llm.base.ChatClient on Claude.

AnthropicFactory dataclass

One :class:AnthropicChatClient per role, with the role's model.

system_and_body

system_and_body(messages: Sequence[ChatMessage]) -> tuple[str | None, list[dict[str, str]]]

The system prompt apart, and the turns as the Messages API wants them.

limen_agents.llm.openai

OpenAI, or any OpenAI-compatible endpoint, through the openai SDK (extra providers).

OpenAIChatClient dataclass

A :class:~limen_agents.llm.base.ChatClient on chat completions.

OpenAIFactory dataclass

One :class:OpenAIChatClient per role.

limen_agents.llm.foundry

Microsoft Foundry: Claude or Azure OpenAI, whichever has an endpoint and a key.

With both, Claude wins, as in the global order of :mod:limen_agents.llm.resolver.

limen_agents.llm.selfhosted

Self-hosted engines with an OpenAI-compatible endpoint: llama.cpp (llama-swap, a LiteLLM gateway) and Ollama, host or cloud. Plain HTTP, no SDK.

LlamaCppChatClient dataclass

llama.cpp, or a gateway in front of it that may route some models to paid APIs.

OllamaChatClient dataclass

Ollama on a host (no key) or Ollama Cloud (a bearer key).

payload

payload(model: str, messages: Sequence[ChatMessage], *, temperature: float, max_tokens: int | None, response_format: str) -> dict[str, Any]

The body of an OpenAI-compatible chat completion.

limen_agents.llm.stub

A chat client that never reaches the network, for tests and for running without a model.

StubChatClient dataclass

Returns canned responses in order, then default; calls records every request.

StubLlmClientFactory dataclass

One :class:StubChatClient per role, built once.