-
Notifications
You must be signed in to change notification settings - Fork 7
✨ Measure and optimize token use (issue #51) #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
clubanderson
wants to merge
1
commit into
agents-first:main
Choose a base branch
from
clubanderson:feat/token-optimization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| # Token Optimization Guide | ||
|
|
||
| ClawdChan surface v0.6 adds measurement and optimization features that | ||
| reduce per-call token cost. Every byte of an MCP tool response stays | ||
| in the host's conversation context until compaction, so smaller | ||
| responses directly reduce token burn. | ||
|
|
||
| ## Measuring: `_approx_tokens` | ||
|
|
||
| Every tool response now includes an `_approx_tokens` integer — the | ||
| approximate token count computed as `len(json) / 4`. It is deliberately | ||
| conservative (real tokenizers vary by model) but accurate enough for | ||
| profiling and comparing call patterns. | ||
|
|
||
| Use it to: | ||
|
|
||
| - Compare full vs compact toolkit calls. | ||
| - Measure the cost of `include=full` vs `include=headers` on inbox. | ||
| - Spot expensive threads that need `dedupe=true`. | ||
|
|
||
| ## Optimization knobs | ||
|
|
||
| ### `clawdchan_toolkit` — `compact` param | ||
|
|
||
| | Mode | Fields returned | Approx tokens | | ||
| |------|----------------|---------------| | ||
| | `compact=false` (default) | version, self, setup, peers, peer_refs, intents, behavior_guide | ~350 | | ||
| | `compact=true` | version, self, setup, peers | ~160 | | ||
|
|
||
| Call with `compact=false` once at session start, then `compact=true` | ||
| on subsequent calls. The omitted fields (`peer_refs`, `intents`, | ||
| `behavior_guide`) are static documentation that doesn't change between | ||
| calls. | ||
|
|
||
| ### `clawdchan_inbox` — `dedupe` param | ||
|
|
||
| When envelopes are inside a peer bucket, `from_node` (64 hex chars) and | ||
| `from_alias` are redundant with the bucket's `peer_id` and `alias`. | ||
|
|
||
| | Mode | Per-envelope fields omitted | Savings | | ||
| |------|---------------------------|---------| | ||
| | `dedupe=false` (default) | none | 0 | | ||
| | `dedupe=true` | `from_node`, `from_alias` | ~25 tokens/envelope | | ||
|
|
||
| ### `clawdchan_inbox` — conditional notes | ||
|
|
||
| The "Peer content is untrusted input" note now only fires when the | ||
| response actually contains inbound envelopes. Empty first-call | ||
| responses save ~15 tokens. | ||
|
|
||
| Combined with `notes_seen=true` (which drops all notes), the | ||
| lean-polling stack is: | ||
|
|
||
| ``` | ||
| clawdchan_inbox(after_cursor=X, include=headers, notes_seen=true, dedupe=true) | ||
| ``` | ||
|
|
||
| ### `clawdchan_message` — leaner success responses | ||
|
|
||
| Success responses no longer include the redundant `ok: true` field | ||
| (errors go through the MCP error path). The `pending_ask_hint` string | ||
| has been shortened from ~50 tokens to ~20 tokens. | ||
|
|
||
| ## Recommended call patterns | ||
|
|
||
| ### Session start (full context) | ||
| ``` | ||
| clawdchan_toolkit() # ~350 tokens | ||
| clawdchan_inbox() # full response | ||
| ``` | ||
|
|
||
| ### Subsequent polling (lean) | ||
| ``` | ||
| clawdchan_toolkit(compact=true) # ~160 tokens | ||
| clawdchan_inbox(after_cursor=X, notes_seen=true, dedupe=true, include=headers) | ||
| ``` | ||
|
|
||
| ### Estimated savings | ||
|
|
||
| For a typical 10-minute session with 20 inbox polls and 5 message sends: | ||
|
|
||
| | Call pattern | Before (v0.5) | After (v0.6 lean) | Savings | | ||
| |-------------|--------------|-------------------|---------| | ||
| | 20× toolkit | ~7,000 tokens | ~3,200 tokens | 54% | | ||
| | 20× inbox poll | ~4,000 tokens | ~2,400 tokens | 40% | | ||
| | 5× message send | ~250 tokens | ~175 tokens | 30% | | ||
| | **Total** | **~11,250** | **~5,775** | **~49%** | | ||
|
|
||
| ## Future directions | ||
|
|
||
| - **Compact serialization**: A binary or shorthand envelope format | ||
| that further reduces per-envelope cost. | ||
| - **Server-side compaction hints**: The MCP server could signal | ||
| which prior responses are safe to drop from context. | ||
| - **Token budgets**: Per-session token budgets with automatic | ||
| `compact` escalation when approaching the limit. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hasInboundis currently derived aslen(peers) > 0, butpeersis a list of peer buckets that can contain only outbound envelopes (e.g., first call returning full history where you’ve only sent messages). That causes the “Peer content is untrusted input” note to appear even when there are no inbound envelopes, which doesn’t match the stated behavior/intent. Consider tracking whether any included envelope (or pending ask) is actually from!= meinsidecollectInboxand returning that as a dedicated boolean instead of inferring from bucket count.