Skip to content

Add token-efficiency comparison script (native vs trajectory vs Harbor ATIF) - #23

Open
sarahwooders wants to merge 4 commits into
mainfrom
sarah/token-efficiency-script
Open

Add token-efficiency comparison script (native vs trajectory vs Harbor ATIF)#23
sarahwooders wants to merge 4 commits into
mainfrom
sarah/token-efficiency-script

Conversation

@sarahwooders

Copy link
Copy Markdown
Contributor

Summary

Adds scripts/token-efficiency/ — a reproducible way to measure how many tokens a real agent session costs in its native format vs this repo's normalized trajectory format vs Harbor's ATIF (RFC 0001).

export ANTHROPIC_API_KEY=...
bun scripts/token-efficiency/index.ts ~/.claude/projects/<project>/<session-id>.jsonl
  • Takes a Claude Code session or Codex rollout path (source auto-detected, --source overrides)
  • Counts tokens via the Anthropic count-tokens API (claude-opus-4-8 by default, chunked at 500K chars)
  • Writes each representation to token-efficiency-out/<session-stem>/ and prints per-row paths
  • --untruncated adds a trajectory row with tool-result truncation disabled

ATIF via Harbor's own converters

Harbor has no standalone conversion CLI, so harbor_atif_convert.py drives the exact upstream ClaudeCode/Codex._convert_events_to_trajectory code from a harbor checkout (auto-cloned to ~/.cache/trajectory/harbor-repo, HARBOR_REPO overrides) with harness-only imports stubbed — only uv + pydantic needed. Output validates clean against harbor's own ATIF validator (harbor-atif2otel) and the RFC's normative rules.

Example (small codex rollout)

format                          bytes       tokens  vs native
native                        168,592       77,281          —
trajectory                     46,763       18,438       4.2x
atif (harbor, minified)        88,410       35,847       2.2x
atif (harbor, persisted)       92,293       36,841       2.1x

On large real sessions (~18.8MB Claude Code / ~18.5MB Codex), trajectory lands at 7.0–8.5× vs native while Harbor ATIF lands at 1.4–2.4× — ATIF intentionally keeps untruncated tool results, structured result payloads, and per-step token metrics, so the gap reflects content policy, not format syntax (details in the folder README).

Test plan

  • bun run typecheck clean
  • Verified end-to-end on a Claude Code session and a Codex rollout, including the fresh-clone path for the harbor checkout

Takes a Claude Code session or Codex rollout path and reports token counts
(via the Anthropic count-tokens API) for the native file, the normalized
trajectory JSONL, and a content-matched Harbor ATIF projection, with
reduction factors vs native. Source is auto-detected; --untruncated adds a
row with tool-result truncation disabled.
Harbor has no standalone conversion CLI, so scripts/harbor_atif_convert.py
drives its exact ClaudeCode/Codex _convert_events_to_trajectory code from a
harbor checkout with the harness-only imports stubbed (only pydantic needed,
run via uv). token-efficiency.ts now reports Harbor-produced ATIF (minified
and as-persisted) instead of the content-matched projection, auto-cloning
harbor to ~/.cache/trajectory/harbor-repo on first use (HARBOR_REPO
overrides).
Outputs native/trajectory/atif files under token-efficiency-out/<session-stem>/
(override with --out-dir) and prints the path per row; directory gitignored.
@cursor

cursor Bot commented Jul 23, 2026

Copy link
Copy Markdown

Bugbot is not enabled for your account, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

@cpacker cpacker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make the reference converter version reproducible. ensureHarborCheckout() shallow-clones whatever the upstream default branch contains on first use, then indefinitely reuses whatever checkout happens to be cached. There is no pinned revision, refresh behavior, or recorded SHA, so two runs labeled as the same comparison can use different converter implementations.

Please pin or explicitly accept a reference revision and print/store that SHA with the results. Recording the trajectory revision and counting model alongside it would make the generated comparison auditable.

👾 Generated with Letta Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants