Skip to content

feat: Add console warning when setWidgetState/data-llm exceeds 4K tokens#384

Open
udaykakade25 wants to merge 1 commit intoalpic-ai:mainfrom
udaykakade25:fix356
Open

feat: Add console warning when setWidgetState/data-llm exceeds 4K tokens#384
udaykakade25 wants to merge 1 commit intoalpic-ai:mainfrom
udaykakade25:fix356

Conversation

@udaykakade25
Copy link
Contributor

@udaykakade25 udaykakade25 commented Jan 22, 2026

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.

  • 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

Test

All Tests Passed including 4k Token Warning
fix3563
How warning is given:
fix3562
fix3561

Greptile Summary

Adds token limit warnings to help developers keep widget state payloads under 4K tokens (~16K characters). When setWidgetState or data-llm content exceeds this limit, a console.warn displays guidance to reduce payload size.

Changes:

  • Created warnIfExceedsTokenLimit utility with ~4 chars/token estimation
  • Integrated warnings into both McpAppAdaptor.setWidgetState and AppsSdkAdaptor.setWidgetState
  • Added specific data-llm warning in data-llm.tsx:35
  • Comprehensive test coverage for warning behavior

Issue Found:

  • Global hasWarned flag causes sequential calls (like in data-llm.tsx line 35 then 37 via setWidgetState) to suppress the second warning, potentially hiding issues with the complete widget state size

Confidence Score: 3/5

  • Safe to merge with minor fix needed for warning suppression logic
  • Core functionality works correctly with good test coverage, but the global hasWarned flag creates a logic error in data-llm.tsx where sequential calls suppress important warnings. This won't cause runtime errors but reduces the effectiveness of the feature
  • packages/core/src/web/helpers/warn-token-limit.ts needs attention for the warning suppression logic

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>
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

5 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

const CHARS_PER_TOKEN = 4;
const CHAR_LIMIT = TOKEN_LIMIT * CHARS_PER_TOKEN;

let hasWarned = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

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:

Suggested change
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.

@udaykakade25
Copy link
Contributor Author

Hey @fredericbarthelet,
I used Claude extensively to add this functionality.
You should definitely take a look :)

? stateOrUpdater(this._widgetState)
: stateOrUpdater;

warnIfExceedsTokenLimit(newState, "setWidgetState");
Copy link
Contributor

Choose a reason for hiding this comment

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

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;
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be worth considering a small tokenizers counter lib like tokenx, WDYT ?


export function warnIfExceedsTokenLimit(
state: unknown,
source: "setWidgetState" | "data-llm",
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think you need this if you use warnIfExceedsTokenLimit.caller.name in your implementation

Copy link
Contributor

@fredericbarthelet fredericbarthelet left a comment

Choose a reason for hiding this comment

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

Hey @udaykakade25 thanks for your proposal. I made a few comments. WDYT?

@fredericbarthelet
Copy link
Contributor

Hey @udaykakade25
Sorry for the ping, but we'd really like to ship this feature soon :)
Did you had a chance to take a look at my comments ?

Thanks 🙏

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.

Add console.warning and devtools warning when setWidgetState/data-llm is called with more than 4K tokens

2 participants