Improve remote site handling: prefetch plan and truncate large API responses#3011
Improve remote site handling: prefetch plan and truncate large API responses#3011
Conversation
| name: activeSite.name, | ||
| url: activeSite.url ?? '', | ||
| id: activeSite.wpcomSiteId!, | ||
| planSlug: activeSite.planSlug, |
There was a problem hiding this comment.
Are there cases where the plan is undefined and we need a fallback (I don't know wp.com enough)
apps/cli/ai/wpcom-tools.ts
Outdated
|
|
||
| // Response too large — return top-level keys and a hint | ||
| const keys = typeof result === 'object' && result !== null ? Object.keys( result ) : []; | ||
| const truncated = json.slice( 0, MAX_RESPONSE_CHARS ); |
There was a problem hiding this comment.
So this is a string truncation, it probably results in invalid json, does that matter or cause any issues?
There was a problem hiding this comment.
I don't think so, the agent reads it as text, not parsing it programmatically 🤔 . The hint appended at the end explicitly says the response is truncated, lists the top-level keys, and tells the agent to retry with a fields parameter.
youknowriad
left a comment
There was a problem hiding this comment.
Still testing, but it's working well so far.
|
I'm still seeing this kind of errors from time to time. doesn't seem to impact the behavior
|
Where can I see this context? |
📊 Performance Test ResultsComparing 4f69fa6 vs trunk app-size
site-editor
site-startup
Results are median values from multiple test runs. Legend: 🟢 Improvement (faster) | 🔴 Regression (slower) | ⚪ No change (<50ms diff) |
@youknowriad did you get a way to reproduce constantly?
It's injected in the enriched prompt that the agent receives at the start of each turn in commands/ai/index.ts line 377. It shows as |
|
I reproduce constantly by picking a site that I have with a paid plan on the wp.com site list One of the first (not the first one) requests done results in |
…sponses Two improvements to the remote WordPress.com site experience in `studio ai`: 1. **Prefetch plan at site selection**: Include `plan` in the `/me/sites` request so it's available when the site is selected. The agent no longer needs a separate API call to check the plan — it arrives in the prompt context alongside the site name and URL. 2. **Truncate oversized API responses**: When a `wpcom_request` response exceeds 80K characters (~20K tokens), truncate it and append a hint telling the agent to use the `fields` query parameter. This prevents the SDK's 25K token MCP output limit from triggering a file-save fallback that breaks the conversation flow. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…ds, lower limit The Claude Agent SDK counts actual tokens (not chars/4) for responses over 50K chars. JSON is token-dense (~3 chars/token), so the previous 80K char limit could exceed the 25K token limit. This fixes it with three changes: - Compact JSON (no indentation) — ~20-30% fewer chars on every response - Strip noisy fields (_links, _embedded, guid, rendered when raw exists) — removes bloat that isn't useful to the agent - Lower truncation limit to 50K chars (~16.7K tokens, safely under 25K) - Tool description now nudges agent to use "fields" and "per_page" upfront Tested: 35% reduction on /posts endpoint. No more "exceeds maximum allowed tokens" errors. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
5f1bba8 to
4a38704
Compare
|
Pushed some further improvements, I am not seeing
|
|
Pushed some more improvements! I couldn't hit exceeds maximum allowed tokens on my testing so far:
|
| 'comment_status', | ||
| 'generated_slug', | ||
| 'permalink_template', | ||
| ] ); |
There was a problem hiding this comment.
There are situations where we need these fields, this is a bit dangerous IMO. Like if we want to retrieve the global styles we need the links... It's also a bit random, we seem to be stripping random fields from all requests.
There was a problem hiding this comment.
The idea is to strip them only when results exceed the limit, and note which ones were stripped and why in the reply so the agent is aware and can ask again with some filtering if it needs the full response.
There was a problem hiding this comment.
note which ones were stripped and why in the reply so the agent is aware and can ask again with some filtering if it needs the full response
Related issues
How AI was used in this PR
Pair-programmed with Claude Code. All code reviewed and tested manually against a live WordPress.com site.
Proposed Changes
Two improvements to the remote WordPress.com site experience in
studio ai:Prefetch plan at site selection: Include
planin the/me/sitesAPI request so it's already available when the user selects a remote site. The agent receives the plan in its prompt context (e.g.,plan: free_plan) and no longer needs a separateGET /call — which returned ~103K chars and exceeded the SDK's MCP output token limit.Truncate oversized API responses: When any
wpcom_requestresponse exceeds 80K characters (~20K tokens), truncate it and append a hint telling the agent to use thefieldsquery parameter. This is a generic safety net — the SDK's limit is 25K tokens (MAX_MCP_OUTPUT_TOKENS), and at ~4 chars/token, 80K chars stays safely under it.Testing Instructions
npm install && npm run cli:buildstudio ai, select a remote WordPress.com siteGET /call to check the planplan: free_plan)Pre-merge Checklist
🤖 Generated with Claude Code