Models, budgets and limits

View Source

ExAgent uses official stock ReqLLM as its general backend. Custom adapters implement ExAgent.Model; the Test adapter runs locally. A model being present in a catalogue is distinct from its tools, output and streaming profile being qualified for ExAgent.

Resolve a text model

The application supplies credentials. This example resolves configuration; calling ExAgent.run/3 with it makes a real provider request:

{:ok, model} = ExAgent.Model.resolve({:openai, id: "gpt-4o-mini"},
  api_key: System.fetch_env!("OPENAI_API_KEY")
)
agent = ExAgent.new(model: model, instructions: "Be concise.")

Use instance credentials for explicit ownership. ReqLLM's startup environment loading is described in Getting started. Never place a key in a prompt or persist the complete live model.

Catalogue updates can remove a model or change its prices, limits and capabilities. ExAgent 2.2.4 requires ReqLLM 1.30 and updates the checkout lock to LLMDB 2026.11.2. Some former OpenRouter entries, including unbiased/pareto and inclusionai/ling-3.0-flash-fin, are no longer present. Resolving an absent ID can still succeed as an unverified model without catalogue metadata. Do not interpret that success as tool qualification or known pricing.

The 2026.11.2 snapshot adds 434 model IDs and removes 258 relative to 2026.10.7; prices, limits and capabilities also change for retained models. The retired OpenRouter IDs include minimax/minimax-m2, inception/mercury-2 and openai/gpt-4-turbo. A library's lockfile does not constrain consuming applications. After adopting the updated ExAgent manifest, update the application lock with mix deps.update exagent req_llm jsv ecto llm_db zoi nimble_options and review the resulting model metadata.

Applications that need stable metadata can pass an explicit model map to ExAgent.Model.resolve/2 or ExAgent.Models.ReqLLM.new/1, with the provider, exact id, verified capabilities, limits, cost and wire protocol they rely on. Maintain those facts against the actual provider; ExAgent does not restore retired catalogue entries or invent prices. Review a model change before resuming persisted work, and keep continuation mismatch checks intact.

ReqLLM 1.29 can merge provider_options from its application configuration or the resolved model's extra metadata. ExAgent rejects non-empty defaults with {:unsupported_options, :provider_defaults} before IO and continuation binding: these options would otherwise bypass the instance allowlist and persisted route. Remove those defaults and pass supported options explicitly as ExAgent.Models.ReqLLM.new(provider_options: ...). Empty defaults are accepted.

Configure Chat tools and streaming

The explicit profile binds the protocol, tools and reasoning capability:

model = ExAgent.Models.ReqLLM.new(
  model: %{
    provider: :openai,
    id: "gpt-4o-mini",
    capabilities: %{tools: %{enabled: true}, reasoning: %{enabled: false}},
    extra: %{wire: %{protocol: "openai_chat"}}
  },
  api_key: System.fetch_env!("OPENAI_API_KEY"),
  tool_profile: :chat_tools_v1
)

These declarations configure admission; they do not prove the endpoint behaves correctly. Qualify the exact gateway/model combination you deploy. The supported profiles describe the available tool/output combinations. Unrepresented reasoning, modality and continuation combinations reject explicitly.

Native output is separate: output_profile: :chat_json_schema_v1 plus agent output_mode: :native. See Tools and output.

Route OpenRouter Chat tools explicitly

Use tool_profile: :openrouter_chat_tools_v1 for a qualified OpenRouter Chat endpoint. The host supplies the exact model ID and truthful capabilities:

model = ExAgent.Models.ReqLLM.new(
  model: %{
    provider: :openrouter,
    id: qualified_model_id,
    capabilities: %{tools: %{enabled: true}, reasoning: %{enabled: false}},
    extra: %{wire: %{protocol: "openai_chat"}}
  },
  api_key: System.fetch_env!("OPENROUTER_API_KEY"),
  tool_profile: :openrouter_chat_tools_v1,
  provider_options: [
    openrouter_provider: %{only: [qualified_provider_id], allow_fallbacks: false}
  ]
)

Routing admits order, only, ignore, allow_fallbacks and require_parameters. Provider lists contain 1–32 bounded identifiers. Optional app_title and app_referer are attribution; other wire settings remain closed. The mandatory tool argument envelope and validation still apply.

For a reasoning-capable model, reasoning_mode: :none additionally requires declared support for effort "none" and disabling thinking. It sends one max_tokens budget. Declaring those capabilities does not establish real provider compatibility. This tools profile does not enable native JSON output.

The route and reasoning mode are bound before the first request and in tool history. Changing them requires a fresh conversation; an existing approval or uncertain request cannot resume through another route. Earlier unbound OpenRouter history is rejected. See Migration for continuation version 4 and the unaffected OpenAI history versions.

Keep reasoning on with OpenRouter tools

Some models cannot turn reasoning off; Claude 5.x on OpenRouter rejects requests that try. Use reasoning_mode: :enabled to run tools with reasoning on:

model = ExAgent.Models.ReqLLM.new(
  model: %{
    provider: :openrouter,
    id: qualified_model_id,
    capabilities: %{
      tools: %{enabled: true},
      reasoning: %{enabled: true, effort: %{supported: true, values: ["low", "medium", "high"]}}
    },
    extra: %{wire: %{protocol: "openai_chat"}}
  },
  api_key: System.fetch_env!("OPENROUTER_API_KEY"),
  tool_profile: :openrouter_chat_tools_v1,
  reasoning_mode: :enabled,
  reasoning_effort: :medium,
  provider_options: [
    openrouter_provider: %{only: [qualified_provider_id], allow_fallbacks: false}
  ]
)

The model must declare reasoning.enabled: true. Returned reasoning becomes ExAgent.Message.Part.Thinking parts, and the provider's reasoning_details (including signatures and encrypted blocks) are kept in the response's continuation and replayed unchanged on the next request, as tool loops on these models require. reasoning_effort is optional (:minimal to :xhigh) and must be one of the declared effort values when the model lists them.

These endpoints reject forced tool use, so this mode always sends auto: typed output relies on the loop's output retries when the model answers in text, and on_max_steps: :final still never executes calls on its last step. Pin the upstream provider: one conversation must not move between upstreams that sign reasoning differently. The mode is bound to the conversation like routing; history from another mode is rejected unless foreign_history: :portable is set. OpenAI Chat does not return reasoning to replay, so :enabled applies only to this OpenRouter profile.

Reasoning-signing providers may check that earlier messages did not change. Compaction rewrites older messages and can therefore be rejected by them; keep compaction thresholds high or start a new conversation on those routes. Output budgets usually include reasoning tokens; streaming keeps its max_tokens limit of 4096, which can be too small for high effort.

Send images and documents

ExAgent.run/3 also accepts a list of content parts instead of a string:

ExAgent.run(agent, [
  %{type: "text", text: "Summarize the report and describe the chart."},
  %{type: "image", data: Base.encode64(png), media_type: "image/png"},
  %{type: "file", data: Base.encode64(pdf), media_type: "application/pdf",
    filename: "report.pdf"}
])

Parts are text, image_url (http, https or data URL), image and file (base64 data). The list is checked and stored with string keys before any request; an invalid part fails with {:invalid_prompt_part, index} and an empty list with :invalid_prompt. The model then accepts or rejects each modality before IO: images need :image in the model's input modalities, and PDFs need :pdf and a provider that sends documents (OpenRouter, Anthropic or Google; stock OpenAI Chat accepts only images). Base64 content counts towards max_history_bytes, so raise it for large files.

Cache stable prompt prefixes

prompt_cache lowers the cost of long instructions and growing tool histories without changing what is sent:

model = ExAgent.Models.ReqLLM.new(
  model: openrouter_spec,
  api_key: System.fetch_env!("OPENROUTER_API_KEY"),
  tool_profile: :openrouter_chat_tools_v1,
  prompt_cache: [ttl: "5m"]
)

On OpenRouter it marks the last system message and the newest message with a cache_control breakpoint, which Anthropic and Gemini routes use; ttl is "5m" or "1h". OpenAI caches long prefixes automatically, and prompt_cache: [key: "tenant:42"] sends a stable prompt_cache_key to improve hits. Other providers reject the option before IO. Cache reads and writes show up in result.usage.details when the provider reports them.

Compact long conversations

ExAgent.Compaction.Capability with ExAgent.Compaction.Summary replaces older messages in the request with a summary once the estimated size exceeds a threshold; the canonical history is unchanged. Each request is compacted again from that history, so by default every step past the threshold calls your summarizer and sends a different prefix. Add an ExAgent.Compaction.Memo to reuse the previous summary instead:

# In your supervision tree:
children = [{ExAgent.Compaction.Memo, name: MyApp.CompactionMemo, max_entries: 1024}]

compaction = %ExAgent.Compaction.Capability{
  compactor: ExAgent.Compaction.Summary,
  opts: [
    threshold_tokens: 60_000,
    keep_recent: 8,
    summarize: &MyApp.Summaries.summarize/1,
    memo: MyApp.CompactionMemo
  ]
}

While the stored summary plus newer messages fits the threshold, the request is rebuilt without calling the summarizer, keeping an identical prefix for provider caches. Above it, the summarizer receives the previous summary and only the newly replaceable messages. Entries are keyed by the conversation's first message and are used only when the summarized messages are unchanged; edited or different histories summarize again. The memo is a bounded in-memory cache: an unavailable memo falls back to an ordinary summary, and losing it only costs one extra summarizer call.

Retry and fall back to another model

ExAgent.Models.Fallback retries transient provider failures and then tries the next model:

primary = ExAgent.Models.ReqLLM.new(primary_options ++ [foreign_history: :portable])
backup = ExAgent.Models.ReqLLM.new(backup_options ++ [foreign_history: :portable])

model = ExAgent.Models.Fallback.new(models: [primary, backup], max_retries: 2)
agent = ExAgent.new(model: model, tools: tools)

Only failures that returned no output are retried: HTTP 408, 429, 500, 502, 503, 504 and 529 by default (:retry_statuses). A gateway may still bill an upstream attempt behind a 502 or 504. Connection failures and total timeouts may already have been processed and billed, so they need retry_transport: true. Backoff is exponential with jitter. Each attempt keeps ModelSettings.timeout as its receive timeout, and the run deadline bounds all attempts together. A stream is retried only if it fails before its first event.

A retried request is still one admitted request: request_limit and result.request_count count model requests, not HTTP attempts. Each retry emits [:exagent, :model, :retry] telemetry. An explicit estimate_cost callback receives the Fallback model rather than the one that answered; price by the response's model_name or rely on usage the models report.

Switching models mid-conversation sends one model's responses to another. Models.ReqLLM rejects that by default. With foreign_history: :portable it sends history it cannot replay natively (another model's responses, or its own from another route or reasoning mode) as plain text and tool calls, without their reasoning, signatures or provider metadata, instead of rejecting it. Tool calls keep ExAgent's argument codec, so tool history stays exact. A conversation that only uses one model and route is sent exactly as before. Fallback skips a candidate whose optional preflight reports a history target mismatch, without IO or retry telemetry. A persisted backup conversation can therefore continue with that compatible model. If all candidates mismatch, the request fails; malformed history and other configuration errors remain terminal.

Bound requests and tool calls

agent = ExAgent.new(
  model: "test",
  usage_limits: %ExAgent.UsageLimits{request_limit: 3, tool_calls_limit: 2}
)
{:ok, result} = ExAgent.run(agent, "Hello!")
result.request_count
# => 1

Request and tool counters are host observations. Limits apply before admission of the next operation. Scoped child runs inherit ancestor authority and budgets; a child cannot enlarge the parent's allowance. Parent usage is inclusive of its children, so adding every span's totals would count delegated work twice.

For token/cost dimensions, read ExAgent.UsageLimits and ExAgent.CostGuard. Strict metric accounting requires a finite request limit. Provider-reported or normalized tokens and estimated cost have quality/status fields. Unknown usage or cost is not zero, and an estimated threshold is not a guaranteed invoice ceiling.

Finish at the step limit

By default a run that reaches max_steps fails with :max_steps_exceeded. With on_max_steps: :final, the last allowed step asks for an answer instead:

agent = ExAgent.new(model: model, tools: tools, max_steps: 8, on_max_steps: :final)

In step max_steps the function tools stay declared but cannot be called (the model gets tool_choice: none, or the output tool for structured output), and that request alone ends with a user instruction saying the limit was reached. The instruction is not added to the conversation history. If the model still calls a function tool, the call is not executed (it gets a not_executed return) and the run fails with :max_steps_exceeded. The run never makes more than max_steps model requests.

Separate the timeout boundaries

BoundaryMeaning
ModelSettings.timeoutReceive inactivity timeout; takes precedence over the model's receive setting.
ReqLLM total_timeoutPositive millisecond bound on one buffered model operation.
Run deadlineAbsolute monotonic milliseconds, inherited and narrowed by child scope.
Continuation lease and active-time budgetBounds one owned continuation attempt; not permission to retry an uncertain effect.

Stopping an owned task closes framework resources. It cannot roll back external IO or guarantee cancellation of arbitrary application callbacks. Host retention limits apply after stock decoding; no upstream hard RAM bound before decoding is promised. Migration documents option precedence and failure shapes.

An incomplete response stays in RunError.partial.pending_response, outside executable history. Its duplicate in RunError.reason may carry an explicit payload_omitted marker when that error copy would exceed 4 KiB. Read the cause from the bounded RequestError and diagnostic partial data from RunError; neither authorizes tools or proves that a turn completed. Arbitrary oversized error control still fails the original retention limit.

API: ExAgent.Models.ReqLLM, ExAgent.Models.Fallback, ExAgent.Model, ExAgent.ModelSettings.