Skip to content

Prompt reduction - #270

Open
jgindin wants to merge 6 commits into
mainfrom
prompt-reduction
Open

jgindin wants to merge 6 commits into
mainfrom
prompt-reduction

Conversation

@jgindin

@jgindin jgindin commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Description

Reduce LLM prompt payload size and prune failed turn history

Reduce prefill token volume across system instructions, schemas, layout snapshots, and conversation history:

  • Omit Multimodal & Image-to-UI Guidelines on text-only turns.
  • Minify injected JSON schemas (COMMON_TYPES_SCHEMA and the active catalog) and prune unreferenced catalog $defs entries.
  • Replace the verbose vacation_booking few-shot example with a compact form and action example.
  • Serialize canvas layout snapshots in chatHistory as compact single- line JSON while keeping the editor draft formatted.
  • Exclude failed and cancelled prompt turns from the LLM context on retry while keeping them in chatHistory for the UI.

Pre-launch Checklist

  • I signed the [CLA].
  • I read the [Contributors Guide].
  • I read the [Style Guide].
  • I have added updates to the [CHANGELOG].
  • I updated/added relevant documentation.
  • My code changes (if any) have tests.
  • If my branch is on fork, I have verified that scripts/e2e_test.sh passes.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request optimizes LLM chat history management and token usage by introducing failed/cancelled turn context exclusion, compacting JSON serialization for schemas and history, and dynamically injecting multimodal guidelines into system prompts when image attachments are present. It also adds robust unit tests to verify these behaviors. The review feedback highlights a potential runtime TypeError in ChatCleaner.isUserLayoutSnapshot if msg.content is not a string, and suggests adding a defensive type guard before calling .trim().

Comment thread shell/src/app/chat/chat-cleaner/chat-cleaner.ts Outdated
@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

⚡ A2UI Composer PR Preview

Your automated preview is successfully live (commit 59d3a0f):
👉 Launch PR Preview

Note: This environment will be wiped automatically when the PR is merged or closed.

@jgindin

jgindin commented Oct 16, 2026

Copy link
Copy Markdown
Contributor Author

Browser Verification Evidence: main vs. prompt-reduction

1. Captured :streamGenerateContent Payload Files

The complete raw HTTP request payloads intercepted from the browser (url, method, headers, rawBytes, metrics, and full body JSON including systemInstruction and contents) for all six sample prompts are saved in:

  • main (e94361f4) — 1.5 MB: captured-payloads-main.json
  • prompt-reduction (a28809ad) — 579 KB (-61.4% total file size across all runs): captured-payloads-prompt-reduction.json

2. System Instructions Dialog Screenshots (main vs. prompt-reduction)

![main: System Instructions dialog (129,669 B) showing multi-line indented catalog schema](/usr/local/google/home/jgindin/.gemini/jetski/brain/c8182084-6264-4307-9bc6-c6348e681203/main-instructions-dialog.png)
<!-- slide -->
![prompt-reduction (Text-Only, Include screenshot OFF): System Instructions dialog (45,911 B, -64.6%) showing single-line minified schemas and omitted multimodal guidelines](/usr/local/google/home/jgindin/.gemini/jetski/brain/c8182084-6264-4307-9bc6-c6348e681203/prompt-reduction-instructions-text-only.png)
<!-- slide -->
![prompt-reduction (Multimodal, Include screenshot ON): System Instructions dialog (54,013 B, -58.3%) dynamically including the Multimodal & Image-to-UI Guidelines section](/usr/local/google/home/jgindin/.gemini/jetski/brain/c8182084-6264-4307-9bc6-c6348e681203/prompt-reduction-instructions-multimodal.png)

3. Sample Prompts 1–4 Execution Screenshots (main vs. prompt-reduction)

![main: Workspace after running Prompts 1–4 (167,123 B on Turn 4)](/usr/local/google/home/jgindin/.gemini/jetski/brain/c8182084-6264-4307-9bc6-c6348e681203/main-prompts-1-to-4.png)
<!-- slide -->
![prompt-reduction: Workspace after running Prompts 1–4 (70,048 B on Turn 4, -58.1%)](/usr/local/google/home/jgindin/.gemini/jetski/brain/c8182084-6264-4307-9bc6-c6348e681203/prompt-reduction-prompts-1-to-4.png)

4. Sample Prompt 6 (Cancelled Multimodal Turn Followed by Text-Only Retry)

![main: Workspace after cancelling a multimodal turn and submitting a text-only login card prompt (150,273 B — retains stopped turn and 5,464 B image in contents)](/usr/local/google/home/jgindin/.gemini/jetski/brain/c8182084-6264-4307-9bc6-c6348e681203/main-prompt-6-retry.png)
<!-- slide -->
![prompt-reduction: Workspace after cancelling a multimodal turn and submitting a text-only login card prompt (52,449 B, -65.1% — strips stopped turn and image from contents while keeping UI history intact)](/usr/local/google/home/jgindin/.gemini/jetski/brain/c8182084-6264-4307-9bc6-c6348e681203/prompt-reduction-prompt-6-retry.png)

5. Captured Payload Comparison Summary (Angular Basic Renderer)

Scenario main Request Bytes prompt-reduction Request Bytes main contents Turns prompt-reduction contents Turns prompt-reduction Multimodal Guidelines Included?
Prompt 1 (Text-Only Form & Action) 144,726 B 52,590 B (-63.7%) 1 (formatted snapshot) 1 (compact snapshot) false
Prompt 2 (Follow-Up Incremental Edit) 150,478 B 54,319 B (-63.9%) 3 (formatted model turn + duplicate snapshot) 3 (compact model turn, no duplicate snapshot) false
Prompt 3 (Text-Only Dynamic List) 158,876 B 56,802 B (-64.2%) 5 5 false
Prompt 4 (Include screenshot ON) 167,123 B 70,048 B (-58.1%) 7 (with 3,436 B PNG) 7 (with 3,436 B PNG) true
Prompt 5 (mockup.png Uploaded) 150,197 B 66,339 B (-55.8%) 1 (with 5,464 B PNG) 1 (with 5,464 B PNG) true
Prompt 6a (503 Error + Text-Only Retry) 150,180 B 52,449 B (-65.1%) 1 (leaks failed user text + 5,464 B PNG) 1 (clean retry turn, 0 B PNG) false
Prompt 6b (Stopped Stream + Text-Only Retry) 150,273 B 52,449 B (-65.1%) 3 (leaks stopped turn + 5,464 B PNG) 1 (clean retry turn, 0 B PNG) false

@jgindin
jgindin force-pushed the prompt-reduction branch 2 times, most recently from ba89dbf to aebfe38 Compare October 19, 2026 16:18
@jgindin
jgindin requested review from mikzat and suyangw-g October 19, 2026 16:19

@suyangw-g suyangw-g 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.

Reviewed commit aebfe382 across all 14 modified files. The prompt compaction, catalog $defs pruning, compact snapshot serialization, and failed/cancelled turn context exclusion are well-structured and backed by thorough unit tests. Left 3 inline comments on user snapshot classification in ChatPanelBase, previewUrl MIME header construction in FileIngestionService, and promptId fallback/test coverage in ChatCoordinator, plus 1 minor test assertion nit in chat-panel.spec.ts.

Comment thread shell/src/app/chat/chat-panel/chat-panel-base.ts Outdated
Comment thread shell/src/app/chat/file-ingestion/file-ingestion.service.ts Outdated
Comment thread shell/src/app/chat/chat-coordinator/chat-coordinator.ts
Comment thread shell/src/app/chat/chat-panel/chat-panel.spec.ts Outdated
@jgindin
jgindin requested a review from suyangw-g October 19, 2026 20:35
Add memoized text-only and multimodal system prompt signals behind
buildSystemPrompt({includeMultimodalGuidelines}) in
ChatPromptFactoryService. Include multimodal guidelines in
ChatCoordinator and ChatPanelBase only when active context or staged
inputs contain an image or screenshot, and infer missing file MIME
types in FileIngestionService via a2a-attachments helpers.
Serialize COMMON_TYPES_SCHEMA and the active catalog with compact
JSON.stringify instead of pretty-printed formatJson. Prune unreferenced
$defs entries from the catalog before prompt injection while preserving
transitively referenced definitions and without mutating input objects.
Replace the Simple Example and verbose vacation_booking Complex Form
Example in ChatPromptFactoryService with a compact form and action
example while retaining the dynamic list template example. Document
optional updateDataModel.path updates and clarify root vs. relative
template paths in Data Binding.
Keep activeDraft() and Monaco editor text formatted via formatJson()
while compacting sanitized layout snapshots and model responses written
to chatHistory via JSON.stringify(). Add ChatCleaner
isUserLayoutSnapshot(), stamp isSnapshot on synced layout messages,
track LLM-committed layouts in lastSynchronizedLayout, and compute
componentCount for user snapshots in ChatPanelBase.
Filter out USER prompt messages and attachments paired with failed
(ERROR or parseError) or cancelled (isCancelled) MODEL outcomes in
ChatCoordinator.getFullMessageContext() and systemPrompt while keeping
them in chatHistory for UI rendering. Preserve earlier succeeded turns
when retries reuse promptId, deduplicate adjacent layout snapshots, and
correlate MODEL placeholders by promptId.
Stamp isSnapshot: false on submitted USER prompt messages in
ChatCoordinator.submitPrompt and gate USER snapshot inference on
ChatCleaner.isUserLayoutSnapshot in ChatPanelBase.visibleChatHistory
and getBubbleClass. Construct FileIngestionService previewUrl from the
inferred MIME type and base64 payload, restrict the
findTargetModelMessageIndex fallback to untagged MODEL turns, and
expand unit test coverage.

This branch has not been deployed

No deployments
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