JSON-encodes tool results with a budget cap and an instructive trailer.
Two things this deliberately never does: raise (a result the sandbox
produced is attacker-shaped data, and a tool handler that raises takes
the host's tool loop with it), and measure in graphemes (the budget is a
byte budget — see @bytes_per_token).
Summary
Functions
Encode value as JSON, truncated to roughly max_tokens tokens.
Functions
@spec encode(term(), pos_integer()) :: {:ok, String.t()} | {:error, String.t()}
Encode value as JSON, truncated to roughly max_tokens tokens.
Returns {:ok, json} when the result fits, {:ok, truncated_json_prefix <> trailer} when it does not (the prefix is deliberately not valid JSON,
and the trailer says so), or {:error, reason} when the value cannot be
encoded at all.