Truncate collapses oversized tool outputs to first 2k + last 2k tokens; inputs at or under 6k pass through unchanged. Head/tail can't overlap: >6k tokens means >24k bytes, well over the 16k kept. Boundaries snap to a valid UTF-8 rune start so non-ASCII output never breaks mid-sequence.
(out string)
| 83 | func ResponseReserve(ctxSize int) int { |
| 84 | if r := ctxSize / 8; r > 8000 { |
| 85 | return r |
| 86 | } |
| 87 | return 8000 |
| 88 | } |
| 89 | |
| 90 | // Truncate collapses oversized tool outputs to first 1k + last 3k tokens; |
| 91 | // inputs at or under 6k pass through unchanged. Head/tail can't overlap: |
| 92 | // >6k tokens means >24k bytes, well over the 16k kept. Boundaries snap to a |
| 93 | // valid UTF-8 rune start so non-ASCII output never breaks mid-sequence. |
| 94 | // The marker defers to the spill file tools.Execute names right below it, so |
| 95 | // the model never reads "re-run the command" and "don't re-run it" in one |
| 96 | // result; re-running narrower is only the fallback when no spill happened. |
| 97 | func Truncate(out string) string { |
| 98 | total := Tokens(out) |
| 99 | if total <= ToolOutputCap { |