Build Anthropic Message Batch requests from a tibble of pairs
Source:R/anthropic_batch_api.R
build_anthropic_batch_requests.RdThis helper converts a tibble of writing pairs into a list of Anthropic
Message Batch requests. Each request has a unique custom_id
of the form "ANTH_<ID1>_vs_<ID2>" and a params object
compatible with the /v1/messages API.
Usage
build_anthropic_batch_requests(
pairs,
model,
trait_name,
trait_description,
prompt_template = set_prompt_template(),
reasoning = c("none", "enabled"),
custom_id_prefix = "ANTH",
...
)Arguments
- pairs
Tibble or data frame with at least columns
ID1,text1,ID2,text2. Typically created bymake_pairs,sample_pairs, andrandomize_pair_order.- model
Anthropic Claude model name, for example
"claude-sonnet-4-5","claude-haiku-4-5", or"claude-opus-4-5".- trait_name
Short label for the trait (for example "Overall Quality").
- trait_description
Full-text description of the trait or rubric.
- prompt_template
Prompt template string, typically from
set_prompt_template. The template should embed your full instructions, rubric text, and<BETTER_SAMPLE>tagging convention.- reasoning
Character scalar indicating whether to allow extended thinking; one of
"none"or"enabled". See details above.- custom_id_prefix
Prefix for the
custom_idfield. Defaults to"ANTH"so that IDs take the form"ANTH_<ID1>_vs_<ID2>".- ...
Additional Anthropic parameters such as
max_tokens,temperature,top_p, orthinking_budget_tokens, which will be passed through to the Messages API.
Value
A tibble with one row per pair and two main columns:
- custom_id
Character ID of the form
"<PREFIX>_<ID1>_vs_<ID2>".- params
List-column containing the Anthropic Messages API
paramsobject for each request, ready to be used in therequestsarray of/v1/messages/batches.
Details
The function mirrors the behaviour of
build_openai_batch_requests but targets Anthropic's
/v1/messages/batches endpoint. It applies the
same recommended defaults and reasoning constraints as
anthropic_compare_pair_live:
reasoning = "none":Omitted
temperatureandtop_pvalues are not sent, so the model/provider defaults apply.Default
max_tokens = 768, unless overridden viamax_tokensin....
reasoning = "enabled"(extended thinking):temperaturemust be 1. If you supply a different value in..., this function throws an error.Defaults to
max_tokens = 2048andthinking_budget_tokens = 1024, with the constraint1024 <= thinking_budget_tokens < max_tokens. Violations of this constraint produce an error.
As a result, batches without extended thinking
(reasoning = "none") use model-default sampling. When
you opt into extended thinking (reasoning = "enabled"), Anthropic's
requirement of temperature = 1 is enforced for all batch requests.
See also
llm_submit_pairs_batch(), llm_download_batch_results()
Other batch backends:
anthropic_create_batch(),
anthropic_download_batch_results(),
anthropic_get_batch(),
anthropic_poll_batch_until_complete(),
build_gemini_batch_requests(),
build_openai_batch_requests(),
gemini_create_batch(),
gemini_download_batch_results(),
gemini_get_batch(),
gemini_poll_batch_until_complete(),
llm_download_batch_results(),
llm_resume_multi_batches(),
llm_submit_pairs_batch(),
llm_submit_pairs_multi_batch(),
openai_create_batch(),
openai_download_batch_errors(),
openai_download_batch_output(),
openai_get_batch(),
openai_poll_batch_until_complete(),
openai_upload_batch_file(),
run_anthropic_batch_pipeline(),
run_gemini_batch_pipeline(),
run_openai_batch_pipeline(),
write_openai_batch_file()
Examples
data("example_writing_samples", package = "pairwiseLLM")
pairs <- example_writing_samples |>
make_pairs() |>
sample_pairs(n_pairs = 3, seed = 123) |>
randomize_pair_order(seed = 456)
td <- trait_description("overall_quality")
tmpl <- set_prompt_template()
# Standard batch requests without extended thinking
reqs_none <- build_anthropic_batch_requests(
pairs = pairs,
model = "claude-sonnet-4-5",
trait_name = td$name,
trait_description = td$description,
prompt_template = tmpl,
reasoning = "none"
)
reqs_none
#> # A tibble: 3 × 2
#> custom_id params
#> <chr> <list>
#> 1 ANTH_S17_vs_S12 <named list [3]>
#> 2 ANTH_S19_vs_S15 <named list [3]>
#> 3 ANTH_S01_vs_S15 <named list [3]>
# Batch requests with extended thinking
reqs_reason <- build_anthropic_batch_requests(
pairs = pairs,
model = "claude-sonnet-4-5",
trait_name = td$name,
trait_description = td$description,
prompt_template = tmpl,
reasoning = "enabled"
)
reqs_reason
#> # A tibble: 3 × 2
#> custom_id params
#> <chr> <list>
#> 1 ANTH_S17_vs_S12 <named list [5]>
#> 2 ANTH_S19_vs_S15 <named list [5]>
#> 3 ANTH_S01_vs_S15 <named list [5]>