Skip to contents

Call a model

Usage

hr_call(
  client,
  messages,
  model = NULL,
  max_output = NULL,
  temperature = NULL,
  schema = NULL,
  schema_name = "result",
  trace = NULL,
  label = "call",
  ...,
  effort = NULL,
  draw_index = 0L,
  closing = FALSE
)

Arguments

client

A gr_client.

messages

A list of list(role=, content=) items, or a single string, which is wrapped as one user message. role may be "system", "developer", "user" or "assistant".

model

Overrides the client default.

max_output

Maximum completion tokens. Clamped to the model's limit and to what the context window actually leaves after the prompt. A value that is not a single number (NA, text) warns and uses the model's limit, as NULL does. For a reasoning model (see hr_model_info()) the cap an API request carries also bounds the model's hidden reasoning, so a small cap is sent as at least 2048 tokens (within the model's limit and the context window); otherwise the reasoning can use all of it and the reply come back empty. The reply is then no longer held to the smaller cap by the API, and the extra tokens are billed if the model uses them.

temperature

Sampling temperature; dropped automatically for reasoning models that reject it.

schema

Optional JSON Schema (as a list) requesting structured output.

schema_name

Name for the schema.

trace

Optional gr_trace to record this call into.

label

Short label for the trace entry.

...

Extra top-level body fields.

effort

Reasoning effort for this call, or NULL (the default) to send none. One string, or a character vector named by label prefix, resolved against label as gr_read_spec() describes. Sent only when model is a reasoning model (see hr_model_info()), as reasoning = {effort} on the Responses API and reasoning_effort on Chat Completions; an hr_mock_client() or hr_backend_client() handler finds it in params$effort. Must be named in full, since it follows .... It may also be an hr_effort_escalate() policy, which sends the call at its base effort, or raised when a trigger fired in the stage before this call's; the trace carries the policy's state, so give one.

draw_index

Which draw of a sampled request this is: 0 (the default) for an ordinary call, or 1, 2, ... for repeated draws of the same request. Above 0 it is part of the hr_cache() key, so N identical calls with draw indexes 1 to N are N cached replies rather than one reply N times, and an hr_replay_client() answers draw k with the k-th recorded reply to the request. An hr_mock_client() or hr_backend_client() handler finds it in params$draw_index, and the trace records it in the step's params; at 0 neither carries it, so an unsampled call is unchanged. Must be named in full, since it follows ....

closing

TRUE for a run's closing request, such as the final answer after rounds of reading, which may spend the calls and dollars a governor holds back with reserve_calls and reserve_usd (see hr_governor()). FALSE (the default) for any other request, which is refused once it would reach into them. It changes nothing else: not the request, the trace, or a cache or replay key. Must be named in full, since it follows ....

Value

A gr_result. Never NULL; $text is always a single string, "" on failure. A successful HTTP call that returned no text (a refusal, a content filter) is reported as ok = FALSE, so an empty completion is never passed downstream as evidence. $cached is TRUE when the response came from an hr_cache() or an hr_replay_client() instead of the network. A reply cut off at the output cap keeps ok = TRUE, since its text is what the model wrote, but always carries finish_reason = "length", whatever the provider called it ("max_tokens", "MAX_TOKENS", a Responses API status of "incomplete"); check for it wherever a complete reply matters. Such a reply is not written to an hr_cache(). $served_model, $response_id and $fingerprint are what the provider said about its reply (NA where it said nothing; $model stays the model asked for), and $attempts is how many requests were sent for it, retries included, 0 for a reply from a cache or a recording. A request that would pass one of the run's limits (see hr_governor()) is not sent: it comes back with ok = FALSE, $refused = TRUE, an $error naming the limit and the condition in $refusal, after a condition of class hr_budget_refused is signalled. It is not recorded as a step of the trace.

A request the provider refuses because its prompt is longer than the model's context window (a 400 or 413 whose error body says so in one of the phrasings providers use, such as context_length_exceeded, "maximum context length" or "prompt is too long") comes back as a failed result with the same error text and $error_class = "hr_context_overflow", and $overflow holds estimate_tokens, the local count of the prompt, context_window, the window registered for the model, and the HTTP status. The trace step records the same in its overflow field, and a condition of class hr_context_overflow is signalled. A refusal that reaches hr_call() as an error raised through hr_ellmer_client() or hr_backend_client() is named the same way when its message carries httr2's "HTTP 400" or "HTTP 413" and one of those phrasings. A 400 with any other body is left as it was. hr_options(on_overflow =) can retry such a request once with a smaller prompt your own function makes; see hr_options(). A replay retries a recorded refusal only where the recorded run did.

Examples

cl <- hr_mock_client(function(messages, params) "The answer is 42.")
res <- hr_call(cl, "What is the answer?")
c(ok = res$ok, text = res$text)
#>                  ok                text 
#>              "TRUE" "The answer is 42." 

# A failed call still returns a usable object.
bad <- hr_call(hr_mock_client(function(m, p) stop("network down")), "hi")
c(ok = bad$ok, text = sprintf("<%s>", bad$text), error = bad$error)
#>             ok           text          error 
#>        "FALSE"           "<>" "network down" 

# A reasoning effort reaches a reasoning model, and the handler sees it.
seen <- hr_mock_client(function(m, p) {
  paste("effort:", if (is.null(p$effort)) "none" else p$effort)
})
hr_call(seen, "hi", model = "gpt-5.6-luna", effort = "low")$text
#> [1] "effort: low"
hr_call(seen, "hi", model = "gpt-4o", effort = "low")$text   # not a reasoning model
#> [1] "effort: none"

# Draws of one request, each cached under its own key.
n <- 0
sampler <- hr_mock_client(function(m, p) { n <<- n + 1; paste("draw", n) })
sampler <- hr_cache_client(sampler, hr_cache(file.path(tempdir(), "harnessr-draws")))
vapply(1:3, function(k) hr_call(sampler, "Pick one.", draw_index = k)$text, "")
#> [1] "draw 1" "draw 2" "draw 3"
vapply(1:3, function(k) hr_call(sampler, "Pick one.", draw_index = k)$text, "")  # cached
#> [1] "draw 1" "draw 2" "draw 3"