feat: Add console warning when setWidgetState/data-llm exceeds 4K tokens#384
feat: Add console warning when setWidgetState/data-llm exceeds 4K tokens#384udaykakade25 wants to merge 1 commit intoalpic-ai:mainfrom
Conversation
Add token limit warnings to help developers keep widget state payloads small. When setWidgetState or data-llm content exceeds ~4K tokens (~16K chars), a console.warn is displayed with guidance to reduce payload size. - Add warnIfExceedsTokenLimit utility with token estimation - Integrate warning into McpAppAdaptor.setWidgetState - Integrate warning into AppsSdkAdaptor.setWidgetState - Add specific data-llm warning in data-llm.tsx - Add comprehensive test coverage Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
| const CHARS_PER_TOKEN = 4; | ||
| const CHAR_LIMIT = TOKEN_LIMIT * CHARS_PER_TOKEN; | ||
|
|
||
| let hasWarned = false; |
There was a problem hiding this comment.
logic: Global hasWarned flag causes missed warnings when data-llm.tsx calls this function twice in sequence (line 35 for description, then via setWidgetState on line 37 for full state). First call sets hasWarned = true, preventing the second call from warning about the complete widget state payload.
Consider tracking warnings per source or using a debounce approach instead:
| let hasWarned = false; | |
| let lastWarnedPayload: string | null = null; |
Should warnings be per-source (data-llm vs setWidgetState) or should the second call in data-llm.tsx be removed?
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/core/src/web/helpers/warn-token-limit.ts
Line: 5:5
Comment:
**logic:** Global `hasWarned` flag causes missed warnings when `data-llm.tsx` calls this function twice in sequence (line 35 for description, then via `setWidgetState` on line 37 for full state). First call sets `hasWarned = true`, preventing the second call from warning about the complete widget state payload.
Consider tracking warnings per source or using a debounce approach instead:
```suggestion
let lastWarnedPayload: string | null = null;
```
Should warnings be per-source (data-llm vs setWidgetState) or should the second call in data-llm.tsx be removed?
How can I resolve this? If you propose a fix, please make it concise.|
Hey @fredericbarthelet, |
| ? stateOrUpdater(this._widgetState) | ||
| : stateOrUpdater; | ||
|
|
||
| warnIfExceedsTokenLimit(newState, "setWidgetState"); |
There was a problem hiding this comment.
if implementation is the same in both apps-sdk and mcp adapter, it probably means the implementation should be done in the hook itself (so in packages/core/src/web/hooks/use-widget-state.ts here)
| @@ -0,0 +1,52 @@ | |||
| const TOKEN_LIMIT = 4000; | |||
| const CHARS_PER_TOKEN = 4; | |||
There was a problem hiding this comment.
Might be worth considering a small tokenizers counter lib like tokenx, WDYT ?
|
|
||
| export function warnIfExceedsTokenLimit( | ||
| state: unknown, | ||
| source: "setWidgetState" | "data-llm", |
There was a problem hiding this comment.
I don't think you need this if you use warnIfExceedsTokenLimit.caller.name in your implementation
fredericbarthelet
left a comment
There was a problem hiding this comment.
Hey @udaykakade25 thanks for your proposal. I made a few comments. WDYT?
|
Hey @udaykakade25 Thanks 🙏 |
Closes #356
Description
Add token limit warnings to help developers keep widget state payloads small. When setWidgetState or data-llm content exceeds ~4K tokens (~16K chars), a console.warn is displayed with guidance to reduce payload size.
Test
All Tests Passed including 4k Token Warning



How warning is given:
Greptile Summary
Adds token limit warnings to help developers keep widget state payloads under 4K tokens (~16K characters). When
setWidgetStateordata-llmcontent exceeds this limit, aconsole.warndisplays guidance to reduce payload size.Changes:
warnIfExceedsTokenLimitutility with ~4 chars/token estimationMcpAppAdaptor.setWidgetStateandAppsSdkAdaptor.setWidgetStatedata-llm.tsx:35Issue Found:
hasWarnedflag causes sequential calls (like indata-llm.tsxline 35 then 37 viasetWidgetState) to suppress the second warning, potentially hiding issues with the complete widget state sizeConfidence Score: 3/5
hasWarnedflag creates a logic error indata-llm.tsxwhere sequential calls suppress important warnings. This won't cause runtime errors but reduces the effectiveness of the feature