Token Rewriting
Token Rewriting lets a Radius organization transform the messages sent by Pi before Radius forwards them to an upstream model. It is useful for reducing repetitive or oversized tool results, removing terminal formatting, adding organization-wide context, or redacting known secret patterns.
Rewriting changes the model's input, not the response shown after the model runs.
Install the management skill
Token Rewriting policy management is provided by the radius-api skill in the core Radius extension package:
pi install npm:@earendil-works/pi-radius
Restart Pi or run /reload, then authenticate with /login radius. The rewriter belongs to the organization selected during that login.
Describe the desired transformation to Pi, for example:
Configure Radius Token Rewriting to trim very large bash tool results while preserving errors and the final lines.
The installed skill discovers the current Radius API, reads the active policy, validates changes in the Radius sandbox, and uploads a new version. Radius provides policy helpers for analyzing bash commands, stripping ANSI escape sequences, truncating text, and safely rewriting bash tool results.
How rewriting works
When an organization has an active rewriter, Radius runs it for every model request made through that organization:
- The policy receives the full message context, including the system prompt, messages, and tool descriptions.
- It runs in an isolated sandbox with no network or environment access.
- Radius validates the resulting Pi conversation.
- The validated messages are forwarded to the requested model.
For a fixed model, the rewritten request goes directly to that model. For a Routing Profile, rewriting happens before candidate construction and the Auto Model policy routes the rewritten request. Both the uploaded rewriter and router receive the effective profile as input.profile and may customize behavior using its ID, default model, or thinking cap. Fixed-model requests omit input.profile.
A profile can independently enable compact_git_diff, compact_git_status, and compact_grep through checkboxes on the organization's Profiles page. The three service-owned profiles enable every built-in by default, while new custom profiles begin with them disabled. Enabled built-ins run after the organization rewriter and can also run without a custom rewriter. auto is a hidden alias for balanced and therefore uses the effective balanced configuration.
A rewriter can change message content, message order, and the system prompt, but it must preserve valid Pi message and tool-call relationships. Radius rejects invalid output during policy testing and skips invalid production output with a warning instead of forwarding a damaged conversation. Built-in compaction is conservative: unrecognized output is preserved, small results are not made larger, and prefixing a command with RADIUS_NO_PROCESS=1 requests the original output.
Advanced built-in rule access
Normal built-in processing is controlled by profile checkboxes rather than custom policy code. A custom rewriter can nevertheless import the same versioned, tested rule implementations for exceptional conditional or custom-order behavior:
import {
applyBuiltinRewriteRules,
rewriteGitDiffResult,
rewriteGitStatusResult,
rewriteGrepResult,
} from "#radius/rewrite-rules/v1";
export default {
rewrite(input) {
return applyBuiltinRewriteRules(input, {
enabled: ["compact_git_diff", "compact_git_status"],
});
},
};
The supported rule IDs are compact_git_diff, compact_git_status, and compact_grep. Applying a rule manually and through gateway-managed processing is not guaranteed to be idempotent; manual application is intended for configurations where the corresponding gateway-managed behavior is disabled.
Scope and visibility
A rewriter is organization-wide, so it affects every member using Radius through that organization. It can branch on input.profile?.id when a transformation should apply only to selected profiles. Since the policy receives unsanitized conversation content, keep it small, reviewable, and limited to the transformations you intend.
Radius records an estimated input-token change when rewriting modifies a request. Pi stores that diagnostic with the assistant message in the session transcript, making it possible to verify which turns changed and whether the policy reduced or added input tokens.
Open the organization's Policies page to inspect the active Token Rewriting source, view previous versions, or compare versions. Removing the active rewriter makes requests pass through unchanged.