Skip to content

fix(pi): count standalone usage entries - #1484

Merged
junhoyeo merged 4 commits into
junhoyeo:mainfrom
JPFrancoia:fix/pi-standalone-usage
Oct 26, 2026
Merged

junhoyeo merged 4 commits into
junhoyeo:mainfrom
JPFrancoia:fix/pi-standalone-usage

Conversation

@JPFrancoia

@JPFrancoia JPFrancoia commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Why

Pi's standalone usage entries are a standard, documented Pi session feature. They record model usage outside assistant replies. Pi itself uses them for cache warming through SessionManager.appendUsage().

References:

TokScale currently reads assistant messages but skips these standalone records, so their tokens are missing from totals. This change reads the existing standard format. It does not introduce a custom record type, a provider integration, or Jev-specific parsing.

Human-written explanation

I'm doing this because Pi recently added support for Jev, a decision model. Jev can be used as a model router, and redirect the user message to model A or B (like sol or luna). Very useful to redirect to a smaller model when the user's input is simple.
But tokscale only track the usage of the real model (sol, luna, etc), not Jev. With this PR, Pi extensions can write usage from Jev into the session, and tokscale can read the usage. Voilà!

Changes

  • Parse top-level usage, model, provider, and timestamp fields for standalone usage entries. Accept arbitrary kind values.
  • Preserve session, workspace, title, agent, and existing pricing behavior.
  • Keep copied usage entries deduplicated without colliding with assistant-message identities. Preserve Kimchi's existing session-scoped behavior.
  • Keep Prime Agent's fresh and cached accounting aligned without treating background usage as assistant completions.
  • Bump the shared Pi-format parser version so existing caches are reparsed. Document the standard record format.

Validation

Seven new regressions cover the shared Pi-compatible clients, arbitrary kinds, malformed records, attribution, fork copies, identity collisions, Prime accounting, and cache invalidation.

  • cargo fmt --all -- --check passes.
  • cargo clippy --locked --workspace --all-features -- -D warnings passes.
  • cargo test --locked --workspace --all-features passes: 4,515 tests pass and five existing tests remain ignored.
  • cargo build --locked --release -p tokscale-cli --bin tokscale passes.
  • The installed CLI exactly matches three real classifier calls: 1,302 input tokens and 123 output tokens. Tool continuation does not add another classifier call.
  • The actual terminal UI shows standalone classifier usage as a separate model row alongside normal assistant models.

Source-reported costs keep the existing Pi parser behavior. TokScale still uses its own price resolver, rather than treating Pi catalog estimates as provider-billed charges.

How it looks like

tokscale-pi-usage-2026-10-16

Summary by cubic

Counts Pi's standalone usage session entries so tokens from background model calls (like cache warming) are included in totals instead of being skipped. Also retains the upstream accounting for the other billed operations and their reported costs after merging main.

  • Parses top-level usage, model, provider, and timestamp fields; accepts any kind, skips malformed records, and ignores unrelated metadata that reuses those field names.
  • Adds standalone usage as separate model rows alongside assistant replies, preserving recorded costs and falling back to existing pricing data when absent.
  • Keeps copied usage entries deduplicated without colliding with assistant-message identities, and preserves Kimchi's session-scoped behavior.
  • Keeps Prime Agent's fresh and cached accounting aligned without treating background usage as assistant completions, even when IDs collide with assistant messages.
  • Bumps the shared Pi-format parser version so existing caches are reparsed, and documents the record format in the README and its maintained translations.

Written for commit 03ee902. Summary will update on new commits.

View guided diff

intent(pi-usage): include standard Pi usage records for model calls outside assistant replies.
decision(pi-parser): accept arbitrary usage kinds through the shared Pi-format parser and invalidate cached assistant-only results.
rejected(pi-usage): provider-specific parsing would tie a standard session feature to one router implementation.
constraint(prime-accounting): standalone usage must not enter assistant completion matching, and cached replay must retain positional alignment.
@vercel

vercel Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
tokscale Ready Ready Preview Oct 26, 2026 5:09pm UTC

Request Review

@cubic-dev-ai cubic-dev-ai Bot 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.

All reported issues were addressed across 4 files

Reply to a comment to ask cubic a question or push back. It learns from your replies.

View guided diff | Re-trigger cubic

Comment thread crates/tokscale-core/src/sessions/pi.rs
Comment thread README.md
Comment thread crates/tokscale-core/src/sessions/prime_agent.rs
Comment thread crates/tokscale-core/src/sessions/pi.rs Outdated
intent(pi-parser): retain assistant usage and agent attribution when other entry types reuse the new standalone field names.
decision(pi-parser): tolerate invalid optional standalone fields and infer the provider when its value contains only whitespace.
constraint(prime-accounting): a same-ID background usage entry must not replace an assistant attribution target in fresh or cached accounting.
decision(pi-docs): mirror native usage coverage in the three maintained README translations.

@cubic-dev-ai cubic-dev-ai Bot 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.

All reported issues were addressed across 5 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread README.ja.md Outdated
Comment thread README.ko.md Outdated
Comment thread README.zh-cn.md Outdated
intent(pi): Resolve PR junhoyeo#1484 merge conflicts by merging upstream main without rewriting branch history.
decision(pi): Retain upstream billed operations, reported costs, and legacy model fallback while preserving standalone usage for Prime Agent.
constraint(prime-agent): Standalone usage must keep distinct deduplication identities and stay outside assistant completion matching on fresh and cached reads.
@junhoyeo

Copy link
Copy Markdown
Owner

The parser review found no blocking issue. I pushed the small documentation correction in ab0a8c18: standalone usage examples now include parentId in all four languages. All examples parse and match; 89 focused Pi/Prime/cache tests passed on the corrected commit, along with formatting and diff checks. The three remaining documentation threads are now marked resolved. Waiting for the updated native/platform CI before merging.

@junhoyeo junhoyeo left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Reviewed ab0a8c184a3805a77977bfbe55f069d1f2365869.

The standalone usage handling preserves distinct accounting identities, tolerant optional fields, reported costs, and the shared Pi/Prime cache invalidation. The corrected examples include valid parent links in all four languages. Focused tests passed (50 Pi, 36 Prime, 3 cache), JSONL parse/equality checks passed, and formatting/diff checks passed. Fresh native/platform/lint CI is green on the corrected head, and all review threads are resolved.

No blocking findings remain in this revision.

@junhoyeo
junhoyeo merged commit ddedb17 into junhoyeo:main Oct 26, 2026
21 checks passed
@JPFrancoia

Copy link
Copy Markdown
Contributor Author

Thank you!

This branch was successfully deployed

1 active deployment
Preview — ab0a8c18 Deployed Oct 26, 2026 by vercel[bot]
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