[PR #413] [MERGED] feat(agents): add per-turn cost readout to agents picker #4612

Closed
opened 2026-10-22 04:09:10 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/413
Author: @cha1latte
Created: 5/4/2026
Status: ✅ Merged
Merged: 5/5/2026
Merged by: @SpicyMarinara

Base: main ← Head: feat/agent-cost-readout


📝 Commits (3)

  • 2f925d8 feat(agents): add per-turn cost readout to agents picker
  • 4dcad1b fix(agents): treat empty promptTemplate as no override
  • a81032e fix(agents): show ~0 token badges instead of hiding them

📊 Changes

3 files changed (+202 additions, -38 deletions)

View changed files

📝 packages/client/src/components/chat/ChatSettingsDrawer.tsx (+114 -38)
📝 packages/shared/src/index.ts (+1 -0)
➕ packages/shared/src/utils/agent-cost.ts (+87 -0)

📄 Description

Summary

Adds a small informational readout to the agents picker in the chat settings drawer (roleplay mode) so users can see the rough overhead of their enabled agent loadout before they hit context-window or latency problems.

  • Cumulative pill at the top of the agents section: ~N tokens of agent instructions · ~M extra calls/turn. Flips to amber with a warning icon when the loadout crosses 4 extra calls or 4000 instruction tokens.
  • Per-agent ~N token badge next to each active agent row.
  • Pure cost function estimateAgentLoadCost lives in @marinara-engine/shared.

Why

Right now there's no in-UI signal for the cumulative cost of an enabled agent set. Users only find out the loadout is too heavy when things fail — context overflows on small local models, latency spikes from too many extra calls, errors in the logs. By that point they're toggling agents off one-by-one to find a working set.

This gives a soft self-correcting signal before any of that happens. Informational only, no toggles are blocked.

Approach

  • Token estimate uses the existing chars/4 heuristic that's already scattered across the client (PeekPromptModal, LorebookFormFields, etc.). Marked clearly as approximate. There's a TODO at the heuristic site for swapping in a real tokenizer when one is added.
  • Extra-calls estimate mirrors the batching in packages/server/src/services/agents/agent-pipeline.ts — agents that share (phase, connection) batch into one call. v1 ignores the tool-extraction nuance (tool-using agents technically run alone) — fine for a soft signal.
  • Chat context is intentionally not counted. Each call also carries recent messages, characters, persona, and lorebook entries, so real per-turn token use is higher than instructionTokens. The hover tooltip on the readout calls this out explicitly.
  • Thresholds (4 calls, 4000 tokens) are first-principles defaults, not measured. 4 calls roughly doubles a typical 2-call baseline; 4000 tokens fills ~50% of an 8k local-model context. Tunable if user feedback says otherwise.

Scope

  • Roleplay mode only. Conversation mode hides the agents section entirely (its "agents" are mode-specific features like Autonomous Messaging) and game mode shows a curated 2-toggle allowlist. Both are different design problems; the cost function is reusable if a similar readout makes sense there later.
  • No new dependencies. No tokenizer added.

Files changed

  • packages/shared/src/utils/agent-cost.ts (new) — pure cost function + threshold constants.
  • packages/shared/src/index.ts — re-export.
  • packages/client/src/components/chat/ChatSettingsDrawer.tsx — imports, agentLoadCost memo, cumulative pill, two per-row badges, activeAgentIds wrapped in useMemo to keep the new memo stable. Also a small fix: empty-string promptTemplate in custom configs now falls through to the built-in default (was ??, needed ||).

Known limitations

  • Estimate is approximate by design and labeled ~. Real per-turn usage will be higher because chat context is not counted.
  • Per-agent token count uses prompt-template length only; agent tool definitions are not separately estimated. v1 simplification.
  • Threshold values are gut-feel defaults, not empirically measured against specific small models.

Test plan

  • Cumulative readout shows ~N tokens · ~M extra calls/turn text in standard chat
  • Per-agent ~N badges render with hover tooltip on active agent rows
  • Crossing 4000 tokens or 4 calls flips the pill to amber with an AlertTriangle icon
  • ⓘ tooltip on the cumulative pill explains the estimate is approximate
  • Conversation mode does not render the agents section (unchanged)
  • Game mode shows the curated agent allowlist with no readout (unchanged)
  • Light mode looks right
  • Dark mode looks right
  • Mobile width (~400px) — cumulative readout truncates cleanly without overflow
  • pnpm --filter @marinara-engine/shared run lint passes locally
  • pnpm --filter @marinara-engine/client run lint passes locally

pnpm check currently fails on main due to a pre-existing OutputType.HwPositionWithDuration rename in packages/server/src/services/haptic/buttplug-service.ts. Not introduced by this PR.

Screenshots

image image image

Summary by CodeRabbit

Release Notes

  • New Features
    • Added per-turn cost estimation and live visualization for active agents in chat settings.
    • Cost readout header dynamically updates styling and icons when cost thresholds are exceeded.
    • Per-agent instruction token usage estimates now displayed as badges in agent categories.
    • Informational tooltips reference specific thresholds to help manage overall agent loadout costs.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/413 **Author:** [@cha1latte](https://github.com/cha1latte) **Created:** 5/4/2026 **Status:** ✅ Merged **Merged:** 5/5/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `main` ← **Head:** `feat/agent-cost-readout` --- ### 📝 Commits (3) - [`2f925d8`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2f925d8ccbdb2dfad3909c29889fb93b7112c377) feat(agents): add per-turn cost readout to agents picker - [`4dcad1b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/4dcad1b20918520a15101e98b676fabf9d9b3bf4) fix(agents): treat empty promptTemplate as no override - [`a81032e`](https://github.com/Pasta-Devs/Marinara-Engine/commit/a81032ea8f3db51ac3e69fbd3d1f8fb5fcc07c9f) fix(agents): show ~0 token badges instead of hiding them ### 📊 Changes **3 files changed** (+202 additions, -38 deletions) <details> <summary>View changed files</summary> 📝 `packages/client/src/components/chat/ChatSettingsDrawer.tsx` (+114 -38) 📝 `packages/shared/src/index.ts` (+1 -0) ➕ `packages/shared/src/utils/agent-cost.ts` (+87 -0) </details> ### 📄 Description ## Summary Adds a small informational readout to the agents picker in the chat settings drawer (roleplay mode) so users can see the rough overhead of their enabled agent loadout before they hit context-window or latency problems. - Cumulative pill at the top of the agents section: `~N tokens of agent instructions · ~M extra calls/turn`. Flips to amber with a warning icon when the loadout crosses 4 extra calls or 4000 instruction tokens. - Per-agent `~N` token badge next to each active agent row. - Pure cost function `estimateAgentLoadCost` lives in `@marinara-engine/shared`. ## Why Right now there's no in-UI signal for the cumulative cost of an enabled agent set. Users only find out the loadout is too heavy when things fail — context overflows on small local models, latency spikes from too many extra calls, errors in the logs. By that point they're toggling agents off one-by-one to find a working set. This gives a soft self-correcting signal before any of that happens. Informational only, no toggles are blocked. ## Approach - **Token estimate** uses the existing `chars/4` heuristic that's already scattered across the client (PeekPromptModal, LorebookFormFields, etc.). Marked clearly as approximate. There's a `TODO` at the heuristic site for swapping in a real tokenizer when one is added. - **Extra-calls estimate** mirrors the batching in `packages/server/src/services/agents/agent-pipeline.ts` — agents that share `(phase, connection)` batch into one call. v1 ignores the tool-extraction nuance (tool-using agents technically run alone) — fine for a soft signal. - **Chat context is intentionally not counted.** Each call also carries recent messages, characters, persona, and lorebook entries, so real per-turn token use is higher than `instructionTokens`. The hover tooltip on the readout calls this out explicitly. - **Thresholds (4 calls, 4000 tokens) are first-principles defaults**, not measured. 4 calls roughly doubles a typical 2-call baseline; 4000 tokens fills ~50% of an 8k local-model context. Tunable if user feedback says otherwise. ## Scope - **Roleplay mode only.** Conversation mode hides the agents section entirely (its "agents" are mode-specific features like Autonomous Messaging) and game mode shows a curated 2-toggle allowlist. Both are different design problems; the cost function is reusable if a similar readout makes sense there later. - No new dependencies. No tokenizer added. ## Files changed - `packages/shared/src/utils/agent-cost.ts` (new) — pure cost function + threshold constants. - `packages/shared/src/index.ts` — re-export. - `packages/client/src/components/chat/ChatSettingsDrawer.tsx` — imports, `agentLoadCost` memo, cumulative pill, two per-row badges, `activeAgentIds` wrapped in `useMemo` to keep the new memo stable. Also a small fix: empty-string `promptTemplate` in custom configs now falls through to the built-in default (was `??`, needed `||`). ## Known limitations - Estimate is approximate by design and labeled `~`. Real per-turn usage will be higher because chat context is not counted. - Per-agent token count uses prompt-template length only; agent tool definitions are not separately estimated. v1 simplification. - Threshold values are gut-feel defaults, not empirically measured against specific small models. ## Test plan - [x] Cumulative readout shows `~N tokens · ~M extra calls/turn` text in standard chat - [x] Per-agent `~N` badges render with hover tooltip on active agent rows - [x] Crossing 4000 tokens or 4 calls flips the pill to amber with an `AlertTriangle` icon - [x] ⓘ tooltip on the cumulative pill explains the estimate is approximate - [x] Conversation mode does not render the agents section (unchanged) - [x] Game mode shows the curated agent allowlist with no readout (unchanged) - [x] Light mode looks right - [x] Dark mode looks right - [x] Mobile width (~400px) — cumulative readout truncates cleanly without overflow - [x] `pnpm --filter @marinara-engine/shared run lint` passes locally - [x] `pnpm --filter @marinara-engine/client run lint` passes locally `pnpm check` currently fails on `main` due to a pre-existing `OutputType.HwPositionWithDuration` rename in `packages/server/src/services/haptic/buttplug-service.ts`. Not introduced by this PR. ## Screenshots <img width="202" height="615" alt="image" src="https://github.com/user-attachments/assets/7cfdb902-fba1-4a5a-8eda-bf704e030d26" /> <img width="221" height="415" alt="image" src="https://github.com/user-attachments/assets/96cc4c36-2c2a-4a9b-80c9-2f6b8a6b0679" /> <img width="413" height="556" alt="image" src="https://github.com/user-attachments/assets/513cd93d-018f-42c7-982c-e13a384677b6" /> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes * **New Features** * Added per-turn cost estimation and live visualization for active agents in chat settings. * Cost readout header dynamically updates styling and icons when cost thresholds are exceeded. * Per-agent instruction token usage estimates now displayed as badges in agent categories. * Informational tooltips reference specific thresholds to help manage overall agent loadout costs. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:09:10 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#4612
No description provided.