Skip to contents

A model reads the prompt and writes the reply inside one context window. When the text you want to send is longer than the window allows, harnessr gives you three tools to keep the request inside it:

Every cut can be written to the trace, so the trace records what a run left out.

harnessr does not summarise or compact a conversation. Each call is one request with the messages you pass it, and nothing carries over from the call before. A prompt that is too long is cut by the rules below, and the cut is recorded; nothing is rewritten in fewer words unless you build a step that does it (see Summarising is a step you build).

Every example runs offline with no API key.

Setting up

library(harnessr)
old <- hr_options(verbose = FALSE)

hr_options() returns the values it replaced, and the last chunk of this guide puts them back with hr_options(old).

The examples use a model with a 300-token window, so that a short report overflows it. The arithmetic is the same at 128,000 tokens. The prices are illustrative and appear only so that no unpriced-model warning interrupts the output:

# Illustrative figures, in USD per million tokens.
hr_register_model("small-reader", context_window = 300, max_output = 100,
                  input_usd = 0.5, output_usd = 1.5)

What a call does with a long prompt

hr_call() does not cut a prompt. It counts the prompt’s tokens and holds the reply’s cap to the room the window leaves, less 32 tokens of slack. The stand-in below replies with the cap it was given:

cap_seen <- hr_mock_client(function(messages, params) paste("cap:", params$max_output))
long_prompt <- paste(rep("word", 195), collapse = " ")
hr_count_tokens(long_prompt)
#> [1] 198
hr_call(cap_seen, long_prompt, model = "small-reader")$text
#> [1] "cap: 70"

A 198-token prompt leaves 300 less 198 less 32, so 70 tokens for the reply, not the model’s 100.

When the prompt leaves no room at all, hr_call() sends nothing. The result has ok = FALSE, status 0 and an error that gives the counts:

tr <- hr_trace()
res <- hr_call(cap_seen, paste(rep("word", 400), collapse = " "),
               model = "small-reader", trace = tr)
str(res[c("ok", "status")])
#> List of 2
#>  $ ok    : logi FALSE
#>  $ status: int 0
res$error
#> [1] "Prompt is 403 tokens but 'small-reader' has a 300-token context window, leaving no room for a reply. Segment more aggressively (lower `max_tokens` on the segmenter) or use a larger-context model."

The trace records the failed call as an error, with no tokens spent. To avoid both a squeezed reply and a request that never goes out, decide what the prompt holds before the call: budget it with hr_budget() and fit it with hr_context_fit().

How much room a prompt has: hr_budget()

hr_budget() does the context arithmetic for one model:

bud <- hr_budget("small-reader")
str(bud)
#> List of 6
#>  $ input         : int 170
#>  $ output        : int 100
#>  $ context_window: int 300
#>  $ overhead      : int 0
#>  $ margin        : num 0.1
#>  $ certain       : logi TRUE

The figures come from four steps:

  1. The usable window is the context window less safety_margin, a tenth by default: floor(300 * 0.9) is 270. The margin absorbs the error of counting tokens locally (see vignette("models")).
  2. The reply’s reserve, output, is the reserve_output you give, held between 1 token and the model’s output cap. Without one, it is a quarter of the usable window (output_reserve_share), raised to at least min_output_tokens (256) and held to the model’s output cap. Here a quarter is 67, the floor raises it to 256, and the cap of 100 holds it at 100.
  3. overhead is the tokens of the prompt you count yourself and do not want budgeted: a system prompt, a question, message framing. It is 0 unless you give it.
  4. input is what is left: 270 less 100 less 0, so 170 tokens.

certain is TRUE because the registry knows the model. For a model it has to guess, certain is FALSE and the budget rests on guessed limits.

On a large model the same call leaves far more room. With an explicit reserve and 200 tokens of overhead:

unlist(hr_budget("gpt-4o", reserve_output = 1024, overhead = 200))
#>          input         output context_window       overhead         margin 
#>       113976.0         1024.0       128000.0          200.0            0.1 
#>        certain 
#>            1.0

The three guarantees

hr_budget() promises three things:

  • input is always a positive whole number.
  • input + output + overhead never exceeds the usable window, context_window * (1 - safety_margin).
  • When no positive budget exists, it stops with an error of class gr_budget_error that names what to change. It never returns a negative number for code downstream to loop on.

Before it gives up, it lowers the reply’s reserve to min_output_tokens. On gpt-4, with an 8,192-token window, 7,000 tokens of overhead leave no room for the default reserve of 1,843, so the reserve drops to 256 and 116 tokens are left for input:

unlist(hr_budget("gpt-4", overhead = 7000)[c("input", "output", "overhead")])
#>    input   output overhead 
#>      116      256     7000

At 9,000 tokens of overhead nothing is left, and the error says why:

tryCatch(hr_budget("gpt-4", overhead = 9000),
         gr_budget_error = function(e) conditionMessage(e))
#> [1] "No positive input budget for model 'gpt-4': context 8192, usable after a 10% safety margin 7372, output reserve 256, fixed overhead 9000. Reduce `reserve_output`, shorten the question/system prompt, or use a model with a larger context window."

An overhead that cannot be read as a number is the same error, since budgeting it as zero is the direction that overruns the window. A reserve_output that cannot be read warns with class gr_bad_setting and takes the default reserve, and a safety_margin outside 0 to 0.5 is held to that range with a warning:

tryCatch(hr_budget("gpt-4", overhead = NA),
         gr_budget_error = function(e) class(e)[1])
#> [1] "gr_budget_error"
unlist(suppressWarnings(hr_budget("gpt-4", safety_margin = 0.9))[c("input", "margin")])
#>  input margin 
#> 3072.0    0.5

hr_budget() counts no text. It knows only the model’s limits and the numbers you give it, so the overhead is yours to count, with hr_count_tokens(). It does not check the provider’s count either: a prompt that fits by the local count can still be a few tokens over by the provider’s, which is what the margin is for.

Fitting pieces into a budget: hr_context_fit()

A prompt is usually built from parts: instructions, a question, notes from an earlier step, an excerpt of a document. hr_context_fit() takes them as a named character vector, in the order the prompt carries them, and cuts them until their total fits the budget.

Here is a report whose last paragraph restates the revenue figure, and a prompt built around it:

report <- paste(c(
  "Acme Ltd annual report, fiscal 2024.",
  "The board met eleven times during the year and approved a new audit charter.",
  "Staff numbers rose from 410 to 452, mostly in the logistics division.",
  "Revenue for fiscal 2024 was 45.2 million dollars, up from 38.1 million.",
  "The company opened two warehouses, in Leeds and in Rotterdam.",
  "Energy costs fell by 6 percent after a supplier change in March.",
  "Note 14 restates fiscal 2024 revenue as 44.9 million dollars."),
  collapse = "\n\n")
pieces <- c(
  instructions = "Answer from the report only. Quote the figure you use.",
  question = "What was revenue in fiscal 2024?",
  notes = paste("Earlier reading: revenue figures appear in the results section",
                "and may be restated in the notes. The 2023 figure was 38.1",
                "million dollars."),
  excerpt = report)
hr_count_tokens(pieces)
#> [1]  15  14  40 122
sum(hr_count_tokens(pieces))
#> [1] 191

The pieces hold 191 tokens against a budget of 170. Under the default policy:

fit <- hr_context_fit(pieces, bud$input)
fit[c("fits", "tokens", "cut", "tokens_dropped")]
#> $fits
#> [1] TRUE
#> 
#> $tokens
#> [1] 166
#> 
#> $cut
#> [1] "excerpt"
#> 
#> $tokens_dropped
#> [1] 25
cat(fit$pieces[["excerpt"]])
#> Acme Ltd annual report, fiscal 2024.
#> 
#> The board met eleven times during the year and approved a new audit charter.
#> 
#> Staff numbers rose from 410 to 452, mostly in the logistics division.
#> 
#> Revenue for fiscal 2024 was 45.2 million dollars, up from 38.1 million.
#> 
#> The company opened two warehouses, in Leeds and in Rotterdam.
#> 
#> Energy costs fell by ...[truncated]

The result is a list:

  • pieces: the pieces after cutting, named and in the order given. A piece cut away entirely is "".
  • fits: whether the total is now within the budget.
  • tokens: the total after cutting.
  • cut: the names of the pieces that were cut, in the order they were cut.
  • tokens_dropped: the tokens the cuts saved, the source text removed less the marker put in its place. The trace counts a cut differently (see the trace’s truncate steps), so its figure for the same cut is larger by the marker’s tokens.

The default policy pins question and instructions, so neither is cut. Of the other pieces, the last one in the prompt goes first: the excerpt. It is cut only as far as the total needs, rounded to a whole word, so it loses 25 tokens and keeps its start. The notes are untouched, since cutting the excerpt was enough.

hr_context_fit() counts each piece on its own. With the default counter each count includes 3 tokens for the framing of a message, so the pieces joined into one prompt count a little less, and a fit errs on the safe side:

prompt <- paste(fit$pieces[nzchar(fit$pieces)], collapse = "\n\n")
hr_count_tokens(prompt)
#> [1] 155

When the pinned pieces alone do not fit

A pinned piece is never cut. When the pinned pieces are larger than the budget, hr_context_fit() cuts every other piece to "" and reports fits = FALSE:

tight <- hr_context_fit(pieces, 25)
tight[c("fits", "tokens", "cut")]
#> $fits
#> [1] FALSE
#> 
#> $tokens
#> [1] 29
#> 
#> $cut
#> [1] "excerpt" "notes"

It does not raise an error, and it does not cut a pinned piece to make room. The decision is yours: stop, send the request with a smaller reply reserve, or move to a model with a larger window.

hr_context_fit() also takes the pieces as a named list of single strings, and reads an NA piece as "". It stops with an error of class gr_bad_setting when a piece has no name or two pieces share one, when the policy does not come from hr_context_policy(), or when the budget is not a number of 0 or more.

Choosing what gets cut: hr_context_policy()

A policy says which pieces are never cut, which are cut first, and which part of a cut piece is kept. The default:

hr_context_policy()
#> <hr_context_policy>
#>   pin:          question, instructions
#>   cut first:    -
#>   keep:         head
#>   order merges: FALSE

It has four arguments:

  • pin: names of pieces that are never cut. The default pins "question" and "instructions".
  • cut_order: names of pieces to cut first, in that order. The pieces it does not name are cut after them, the last piece of the prompt first.
  • keep: which part of a cut piece is kept, "head", "tail", "both" or "scored" (see Cutting one piece). One rule applies to every piece; a named vector sets a rule per piece, with one unnamed element as the rule for the rest.
  • order_merges: read only by the document layer’s merge packers, which then place findings by the read’s context_order. hr_context_fit() ignores it.

A name in pin or cut_order that matches no piece is ignored, so one policy can serve prompts with different pieces. A piece with no rule of its own and no unnamed rule is cut with "head", and a piece whose rule is "scored" but that has no scores is cut with "head" too.

p <- hr_context_policy(pin = "question", cut_order = c("notes", "excerpt"),
                       keep = c(excerpt = "both", "tail"))
p
#> <hr_context_policy>
#>   pin:          question
#>   cut first:    notes, excerpt
#>   keep:         excerpt = both, tail
#>   order merges: FALSE

hr_context_policy() checks its arguments. A piece both pinned and in cut_order, two unnamed keep rules, or a rule that does not exist is an error of class gr_bad_setting:

tryCatch(hr_context_policy(pin = "notes", cut_order = "notes"),
         error = conditionMessage)
#> [1] "'notes' is both pinned and in `cut_order`; a pinned piece is never cut."
tryCatch(hr_context_policy(keep = "middle"), error = conditionMessage)
#> [1] "`keep` must be one or more of \"head\", \"tail\", \"both\", \"scored\", named by piece."

One prompt, five policies

The report’s answer depends on its last paragraph. A stand-in for the model shows which figure each prompt carries: it answers with the restated figure when the prompt holds it, else the first figure, else that the figure is missing.

reader <- hr_mock_client(function(messages, params) {
  p <- messages[[length(messages)]]$content
  if (grepl("44.9", p, fixed = TRUE)) "44.9 million dollars, as restated in note 14."
  else if (grepl("45.2", p, fixed = TRUE)) "45.2 million dollars."
  else "The excerpt does not give the figure."
})
ask <- function(fit) {
  prompt <- paste(fit$pieces[nzchar(fit$pieces)], collapse = "\n\n")
  hr_call(reader, prompt, model = "small-reader")$text
}

"scored" needs a score per unit of the excerpt. Here a paragraph scores 1 when it mentions revenue; in a real run the score could come from a retriever’s ranking. Five policies, one budget:

paragraphs <- strsplit(report, "\n\n", fixed = TRUE)[[1]]
scores <- list(excerpt = as.numeric(grepl("revenue", paragraphs, ignore.case = TRUE)))
policies <- list(
  default     = hr_context_policy(),
  notes_first = hr_context_policy(cut_order = "notes"),
  tail        = hr_context_policy(keep = c(excerpt = "tail", "head")),
  both        = hr_context_policy(keep = c(excerpt = "both", "head")),
  scored      = hr_context_policy(keep = c(excerpt = "scored", "head")))
fits <- lapply(policies, function(pol) hr_context_fit(pieces, bud$input, pol, scores = scores))
data.frame(cut = vapply(fits, function(f) paste(f$cut, collapse = ", "), ""),
           dropped = vapply(fits, `[[`, 0, "tokens_dropped"),
           answer = vapply(fits, ask, ""))
#>                 cut dropped                                        answer
#> default     excerpt      25                         45.2 million dollars.
#> notes_first   notes      26 44.9 million dollars, as restated in note 14.
#> tail        excerpt      26 44.9 million dollars, as restated in note 14.
#> both        excerpt      27 44.9 million dollars, as restated in note 14.
#> scored      excerpt      26 44.9 million dollars, as restated in note 14.

Under the default, the excerpt loses its end and with it the restatement, so the model sees only 45.2 million. Each other choice keeps the restatement, and each pays for it somewhere else:

  • notes_first cuts the notes instead and keeps the whole excerpt. The notes lose their end, the 2023 figure among it.
cat(fits$notes_first$pieces[["notes"]])
#> Earlier reading: revenue figures ...[truncated]
  • tail keeps the end of the excerpt and loses the start, where a report names the company and the year.
cat(fits$tail$pieces[["excerpt"]])
#> ...[truncated] numbers rose from 410 to 452, mostly in the logistics division.
#> 
#> Revenue for fiscal 2024 was 45.2 million dollars, up from 38.1 million.
#> 
#> The company opened two warehouses, in Leeds and in Rotterdam.
#> 
#> Energy costs fell by 6 percent after a supplier change in March.
#> 
#> Note 14 restates fiscal 2024 revenue as 44.9 million dollars.
  • both keeps the two ends and loses the middle. Here the middle held the first revenue figure.
cat(fits$both$pieces[["excerpt"]])
#> Acme Ltd annual report, fiscal 2024.
#> 
#> The board met eleven times during the year and approved a new audit charter.
#> 
#> Staff numbers rose from 410 to 452, mostly in
#> ...[truncated]
#> warehouses, in Leeds and in Rotterdam.
#> 
#> Energy costs fell by 6 percent after a supplier change in March.
#> 
#> Note 14 restates fiscal 2024 revenue as 44.9 million dollars.
  • scored keeps the paragraphs with the highest scores that fit, in their original order. The two revenue paragraphs score 1 and the rest 0; tied paragraphs fill the room left, earliest first. The cut is only as good as the scores.
cat(fits$scored$pieces[["excerpt"]])
#> Acme Ltd annual report, fiscal 2024.
#> 
#> The board met eleven times during the year and approved a new audit charter.
#> 
#> Staff numbers rose from 410 to 452, mostly in the logistics division.
#> 
#> Revenue for fiscal 2024 was 45.2 million dollars, up from 38.1 million.
#> 
#> Note 14 restates fiscal 2024 revenue as 44.9 million dollars.
#> 
#> ...[truncated]

No rule is right for every prompt. Keep the head when the start of a piece matters most (a draft, a document’s opening), the tail when the latest text matters most (a running log), both ends for a long document whose summary and conclusion carry the most, and scores when you have a ranking you trust.

Cutting one piece

hr_truncate(): keep the part you choose

hr_truncate() cuts one string to at most n tokens. Six short paragraphs:

txt <- paste("Para", 1:6, "says something about revenue.", collapse = "\n\n")
hr_count_tokens(txt)
#> [1] 55

keep = "head", the default, keeps the start and puts the marker at the end. It is hr_truncate_tokens() itself:

hr_truncate(txt, 20)
#> [1] "Para 1 says something about revenue. ...[truncated]"
identical(hr_truncate(txt, 20), hr_truncate_tokens(txt, 20))
#> [1] TRUE

keep = "tail" keeps the end and puts the marker at the front:

hr_truncate(txt, 20, keep = "tail")
#> [1] "...[truncated] Para 6 says something about revenue."

keep = "both" gives the start and the end about half the budget each, with the marker on a line of its own between them:

cat(hr_truncate(txt, 30, keep = "both"))
#> Para 1 says something about
#> ...[truncated]
#> Para 6 says something about revenue.

keep = "scored" splits the text into units at blank lines, keeps the units with the highest scores that fit, joins them with a blank line in their original order, and adds the marker after a blank line when any unit was dropped. Ties keep the earlier unit:

cat(hr_truncate(txt, 30, keep = "scored", scores = c(1, 9, 2, 8, 3, 4)))
#> Para 2 says something about revenue.
#> 
#> Para 4 says something about revenue.
#> 
#> ...[truncated]

A character vector of more than one element is taken as the units, with no splitting:

hr_truncate(c("first unit here", "second unit here", "third unit here"), 12,
            keep = "scored", scores = c(3, 1, 2))
#> [1] "first unit here\n\nthird unit here"

"scored" without one score per unit is an error, and so is a keep that is not one of the four:

tryCatch(hr_truncate(txt, 20, keep = "scored"), error = conditionMessage)
#> [1] "`keep = \"scored\"` needs `scores`, one number per unit: the text has 6 unit(s) (its elements, or its paragraphs split at blank lines)."

The marker rules

The marker, " ...[truncated]" by default, tells the model that text is missing. The rules:

  • The marker appears only when something was cut. Text that fits comes back unchanged.
  • "head" appends the marker as given. "tail", "both" and "scored" trim its spaces and place it at the front, between the ends, or after the last unit.
  • The marker counts against n. When it would cost half the budget or more, it is dropped and the whole budget goes to text.
  • marker = "" turns it off, and the whole budget goes to text.
  • With "both", when the end has no room the start alone is kept, as "head" keeps it; when no word of the start fits, the end alone is kept.
  • With "scored", when no unit fits beside the marker, the units that fit without it are kept, with no marker.

The result is never longer than n tokens. A long marker on a small budget is dropped:

hr_truncate(txt, 8, marker = " ...[cut here: see the full report]")
#> [1] "Para 1 says"
hr_truncate(txt, 20, keep = "tail", marker = "")
#> [1] "5 says something about revenue.\n\nPara 6 says something about revenue."

hr_truncate_tokens(): the start, cut at a word

hr_truncate_tokens() keeps the start of a string, as written, and adds the marker. Line breaks and indentation stay, so a table keeps its rows:

cat(hr_truncate_tokens("Arm | N | Events\nA | 482 | 31\nB | 479 | 44\nC | 470 | 50", 24))
#> Arm | N | Events
#> A | 482 | ...[truncated]

The cut falls at the end of a word. A unit too long to be a word (a run of CJK text, base64, a long URL: anything over 16 tokens alone) is cut inside, at a character, so a long unbroken run still fills the budget:

hr_truncate_tokens(paste("Key:", strrep("Q", 300)), 30)
#> [1] "Key: QQQQQQQQQQQQQQQQQQQQQQQ ...[truncated]"

n of 0 or less, or blank text, returns ""; n = Inf returns the text unchanged. It never returns NA:

c(hr_truncate_tokens(txt, 0), hr_truncate_tokens("", 10),
  hr_truncate_tokens("short enough already", Inf))
#> [1] ""                     ""                     "short enough already"

Neither function writes anything by itself. hr_truncate() writes to a trace when you give it one; hr_truncate_tokens() takes no trace.

Cuts that say what they removed

A model given part of a list can write as if it had the whole: “five studies report a benefit” when five of the twelve it was shown do, out of forty. Three opt-in settings make a cut visible in the request itself. Each changes the text sent, so each is off by default.

A marker that counts

A marker may hold {tokens_dropped} and {units_dropped}. hr_truncate() fills them after the cut: the tokens of the text it removed, and the units it did not keep whole (the elements of a vector, else the paragraphs):

cat(hr_truncate(txt, 40, marker = " [{tokens_dropped} tokens, {units_dropped} paragraphs cut]"))
#> Para 1 says something about revenue.
#> 
#> Para 2 says something about revenue.
#> 
#> Para 3 says something about [28 tokens, 4 paragraphs cut]

The cut is sized with each count at its largest, so the marker the model sees never costs more than the room set aside for it, and the result stays within n. The marker rules above apply as they do to a plain marker. A policy’s marker is the one hr_context_fit() passes to every cut it makes:

counted <- hr_context_policy(marker = " [{tokens_dropped} tokens cut]")
cat(hr_context_fit(pieces, bud$input, counted)$pieces[["excerpt"]])
#> Acme Ltd annual report, fiscal 2024.
#> 
#> The board met eleven times during the year and approved a new audit charter.
#> 
#> Staff numbers rose from 410 to 452, mostly in the logistics division.
#> 
#> Revenue for fiscal 2024 was 45.2 million dollars, up from 38.1 million.
#> 
#> The company opened two warehouses, in Leeds and in Rotterdam.
#> 
#> Energy costs fell [30 tokens cut]

Stubs for the pieces left out

A piece cut away entirely becomes "", and the model is told nothing of it. With overflow = "stubs", hr_context_fit() lists such pieces by name, with the titles you pass as titles, in at most stub_tokens tokens (200 by default). It does not fetch anything: a name tells the model, or you, what to ask for next. Here eight studies compete for a small budget:

studies <- setNames(paste("Study", 1:8, "found that revenue rose after the price change",
                          "in its region, with figures for each quarter."),
                    paste("study", 1:8))
titles <- setNames(paste("Regional trial", 1:8), names(studies))
stubbed <- hr_context_fit(c(question = "What was found?", studies), 120,
                          hr_context_policy(overflow = "stubs", stub_tokens = 40),
                          titles = titles)
stubbed$omitted
#> [1] "study 4" "study 5" "study 6" "study 7" "study 8"
cat(stubbed$notice)
#> Left out for space:
#> - study 4: Regional trial 4
#> - study 5: Regional trial 5
#> - and 3 more

The list goes in the fit’s notice, which you place in the request; the pieces stay where they were. Room for the notice is set aside before anything is cut, so the pieces and the notice together fit the budget. Stubs that do not fit under stub_tokens are counted in a last line, "- and N more". When stub_tokens cannot hold even one stub, the list is the header and that last line alone. Only unpinned pieces cut away entirely are listed; a piece cut in part carries the marker instead.

One line on the subset

disclose_subset adds one line to the notice when pieces were cut away. TRUE writes “{shown} of {total} items shown; do not state totals.”; a string is used as the template:

shown <- hr_context_fit(c(question = "What was found?", studies), 120,
                        hr_context_policy(
                          disclose_subset = "{shown} of {total} studies shown; do not state totals."))
cat(shown$notice)
#> 4 of 8 studies shown; do not state totals.

{total} counts the unpinned pieces that had text, and {shown} those that still have some. With both settings the line comes first and the stubs follow. When nothing is cut away the notice is "". The room set aside is at most half the budget, so with a very small budget the notice is "" as well: a fit never goes over the budget to name what it left out.

The readers use a policy’s marker for the cuts they make under it. refine fits two pieces and sets no room aside for a notice. gr_synthesise() places the notice under the gaps it cuts; its “Context policy” help section covers how. In a packer of your own, place fit$notice in the request.

Cuts in the trace

Give hr_context_fit() or hr_truncate() a trace, and each cut becomes a step labelled "truncate". hr_context_fit() names each step’s where as "<where>.<piece>":

tr <- hr_trace()
fit <- hr_context_fit(pieces, bud$input, hr_context_policy(cut_order = "notes"),
                      trace = tr, where = "answer")
cuts <- Filter(function(s) identical(s$label, "truncate"), tr$steps)
do.call(rbind, lapply(cuts, function(s) as.data.frame(s$detail)))
#>          where limit tokens_before tokens_kept tokens_dropped piece keep
#> 1 answer.notes    19            40          11             29 notes head
#>   dropped_part
#> 1          end

Each step’s detail holds:

Field What it holds
where The cut site, in the dotted style of call labels.
limit The token count the piece was cut to; 0 for a piece cut away.
tokens_before, tokens_kept, tokens_dropped The piece’s tokens before the cut, after it (the marker left out) and the difference.
piece The piece’s name, or NA.
keep The rule the cut used.
dropped_part "end", "start", "middle", "all", or for "scored" the units dropped, as "units 2, 5".

The two counts of one cut differ. The step above says the notes lost 29 tokens of source text, while fit$tokens_dropped is 26: the fit counts the marker as kept text, so it gives the tokens the cut saved, and the trace gives the source text lost.

A text that fits writes nothing, so a trace with no "truncate" steps had no cuts. The printed trace counts them among its steps, and as.data.frame() of a trace leaves them out, since it lists requests only:

tr
#> <gr_trace run_20261127174431.709_0098a7>  1 steps, 0 model calls, 0 in / 0 out tokens, 0 error(s)
#>   steps: truncate x1 
#>   cost: no cost recorded
#>   time: 0.00 s (requests 0%, local 0%, overhead 100%)
#>   started: 2026-11-27T17:44:31.710Z

Two other parts of the core read or write these steps. hr_embed() cuts a text longer than the embedding model’s window and writes the cut as a "truncate" step whose where is "embed.input". And under hr_effort_escalate(), a "truncate" step in a stage is the "truncated" trigger that raises the next stage’s effort; vignette("effort") covers it.

Summarising is a step you build

The core does not ask a model to shorten a prompt for you. Each call is one request, and the only way the core makes text shorter is a cut you can see in the trace. (The hierarchical reader of the document layer summarises chunks as its way of reading; each summary is a request of its own in the trace.) When you want a summary in place of a cut, such as a long set of notes condensed before the next request, make it a step of its own: a call whose reply becomes a piece of the next prompt.

condense <- hr_step("condense", function(notes, ctx) {
  if (hr_count_tokens(notes) <= 30) return(notes)
  ctx$call(paste("Condense these notes, keeping every figure:\n\n", notes))$text
}, input = "state", output = "state")

Inside a chain, ctx$call() sends the request through the arm’s client and trace, so the summary is a request in the trace like any other, with its own cost. Called here with a stand-in context:

summariser <- hr_mock_client(function(messages, params) {
  "Revenue may be restated in the notes; 2023 was 38.1 million."
})
stand_in <- list(call = function(m, ...) hr_call(summariser, m, model = "small-reader"))
condense(pieces[["notes"]], stand_in)
#> [1] "Revenue may be restated in the notes; 2023 was 38.1 million."

A summary can drop a figure a cut would have kept, so measure it: compare answers with and without the step, as vignette("compose") shows for chains of steps.

When the provider says the prompt is too long

The local count can come in under the provider’s, and the provider then refuses the request. Most providers answer with a 400 whose body names the problem: OpenAI’s code context_length_exceeded, Anthropic’s “prompt is too long”, Gemini’s “exceeds the maximum number of tokens”. hr_call() reads those phrasings and marks the failure with the class hr_context_overflow. It does the same for a refusal that hr_ellmer_client() or hr_backend_client() raises as an error, when the message carries httr2’s “HTTP 400” or “HTTP 413” beside one of the phrasings. The error text stays as the provider sent it, and the result and the trace step keep the local count of the prompt beside the window registered for the model, so you can see how far apart the two counts were. A 400 for any other reason is left as it was.

hr_mock_client(mode = "overflow") refuses its first request that way:

tr <- hr_trace()
over <- hr_mock_client(function(messages, params) "Revenue was 45.2 million.",
                       mode = "overflow")
res <- hr_call(over, "What was revenue?", trace = tr)
c(ok = res$ok, class = res$error_class)
#>                    ok                 class 
#>               "FALSE" "hr_context_overflow"
str(tr$steps[[1]]$overflow)
#> List of 3
#>  $ estimate_tokens: num 7
#>  $ context_window : num 128000
#>  $ status         : int 400

harnessr does not shorten the prompt for you. If you want one more try, give on_overflow a function of your own that returns a smaller prompt. It is called with the messages sent and what the refusal said, and its messages go out once as a new request, which the governor checks and the trace records:

shorter <- function(messages, info) {
  last <- length(messages)
  messages[[last]]$content <- hr_truncate(messages[[last]]$content, 50)
  messages
}
prev <- hr_options(on_overflow = shorter)
over$reset()
tr <- hr_trace()
res <- hr_call(over, "What was revenue?", trace = tr)
c(ok = res$ok, steps = length(tr$steps),
  retry = isTRUE(tr$steps[[2]]$overflow_retry))
#>    ok steps retry 
#>     1     2     1
hr_options(prev)

The first step is the refusal and the second the retry. A retry that is refused again is returned as it is, with no third try. A replay of the trace retries a refusal only where the recorded run did, so set the same on_overflow before you replay a run that retried.

Choosing the settings

Four options decide how much room hr_budget() leaves for a prompt and what happens when a provider refuses one. Each walkthrough says what the option controls, the signs that point to changing it, where its default came from, and how to test a change.

The margin: safety_margin

What it controls. The share of the context window hr_budget() leaves unused, 0.10 by default, held to 0 to 0.5. It absorbs the difference between the local token count and the provider’s. The spending limits use it too: before a request goes out, its prompt count is raised by the margin when the governor checks it against max_cost_usd (see vignette("runs")).

Signs it needs changing. A provider error that a prompt was over the window means the local count came in under the provider’s by more than the margin: raise it, or plug in a tokenizer that matches the model (see vignette("models")). With an exact tokenizer, a smaller margin gives more of the window to text.

Where the default came from. A tenth of the window was set to absorb the error of counting tokens locally, and was not measured against it. gr_settings_table("options") lists it as untested.

How to test it. Compare the input room at each margin you consider, and the local count of your prompts with the prompt tokens your provider reports:

vapply(c(0, 0.05, 0.10, 0.20), function(m) hr_budget("gpt-4o", safety_margin = m)$input,
       numeric(1))
#> [1] 111616 105216  98816  86016

The reply’s reserve: output_reserve_share and min_output_tokens

What they control. When a call to hr_budget() gives no reserve_output, the reply’s reserve is output_reserve_share (0.25) of the usable window, raised to at least min_output_tokens (256) and held to the model’s output cap. Whatever the reserve takes, the prompt cannot use. min_output_tokens is also the floor the reserve drops to when the default reserve leaves no input room. An explicit reserve_output is used as given between 1 token and the model’s output cap, unless it leaves no input room, in which case it drops to the same floor. The readers of the document layer give an explicit reserve, their max_answer_tokens, so output_reserve_share changes only the budgets you compute without one, while min_output_tokens still sets the floor theirs drops to.

Signs they need changing. The reserve is room, not the reply’s cap: hr_call() caps a reply at its own max_output. When you send the reserve as that cap, as the readers do, replies cut off at it (finish_reason "length") point to a larger reserve. A prompt cut while the replies use a small part of their reserve points to a smaller share, which gives the prompt the difference.

Where the defaults came from. A quarter of the window and a floor of 256 tokens were set as room for a short reply and not measured. gr_settings_table("options") lists both as untested.

How to test them. See how the input room moves with the share, on the model you use:

prev <- hr_options()[c("output_reserve_share", "min_output_tokens")]
room <- vapply(c(0.10, 0.25, 0.40), function(s) {
  hr_options(output_reserve_share = s)
  unlist(hr_budget("gpt-4")[c("input", "output")])
}, numeric(2))
colnames(room) <- c(0.10, 0.25, 0.40)
room
#>         0.1 0.25  0.4
#> input  6635 5529 4424
#> output  737 1843 2948
hr_options(prev)

Then run your prompts at the share you pick and check finish_reason and the reply’s token counts in the trace.

A refused long prompt: on_overflow

What it controls. What hr_call() does after a provider refuses a prompt as longer than the window (see When the provider says the prompt is too long). "error", the default, returns the failed result. A function of (messages, info) returns a smaller prompt, which is sent once more.

Signs it needs changing. Steps whose overflow field is set, or results with error_class "hr_context_overflow". Before you add a retry, compare estimate_tokens with context_window in those steps: an estimate well under the window means the local count is off, and a larger safety_margin or a matching tokenizer fixes the cause where a retry only treats it.

Where the default came from. A retry is an extra paid request, so it is yours to ask for. gr_settings_table("options") lists the setting as untested.

How to test it. Run the requests that overflowed with your function set, and check the retried steps’ replies against the ones you expect, as in the example above.

Where the document layer builds on this

The readers size their prompts with hr_budget(). A read takes a policy as the context_policy setting of gr_read_spec(): refine fits its draft and excerpt with hr_context_fit(), and the merges, hierarchical and preview cut their pieces with hr_truncate(). “What a request keeps” in vignette("readers") covers the setting. A write-up takes one as the context_policy argument of gr_synthesise(), for its gaps and its merges; the synthesis settings in vignette("review") cover it.

Next