[Discuss] Optimize context injection for token limits and agent comprehension
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3574925. --> Reported by: [kristen pol](https://www.drupal.org/user/8389) >>> <p>[Tracker]<br> <strong>Update Summary: </strong>[One-line status update for stakeholders]<br> <strong>Check-in Date: </strong>MM/DD/YYYY<br> <strong>Blocked by: </strong>[#XXXXXX] (New issues on new lines)<br> <strong>Additional Collaborators: </strong> @username1, @username2<br> <em>Metadata is used by the <a href="https://www.drupalstarforge.ai/" title="AI Tracker">AI Tracker.</a> Docs and additional fields <a href="https://www.drupalstarforge.ai/ai-dashboard/docs" title="AI Issue Tracker Documentation">here</a>.</em><br> [/Tracker]</p> <h3 id="summary-problem-motivation">Problem/Motivation</h3> <p>Right now, context is injected into end of the prompt without much structure.</p> <p>It's the ID, tags (outdated), and content. It used to have title but then that sometimes caused issues so was removed but that was for TEST content so it probably should be readded.</p> <h3 id="summary-proposed-resolution">Proposed resolution</h3> <p>Some ideas from ChatGPT below. Discuss and figure out next steps.</p> <p>At minimum, we should provide the label, purpose, and scope. In the future, we should probably add a summarization of the context.</p> <h3 id="summary-remaining-tasks">Target date or deadline</h3> <p>February or March 2026</p> <h3 id="summary-remaining-tasks">Remaining tasks</h3> <ul> <li>Discuss</li> <li>Figure out next steps</li> </ul> <h3>ChatGPT's analysis</h3> <p>If the tool is returning raw markdown for brand guidelines / writing standards / governance, the main problems you&rsquo;ll hit are:</p> <ul> <li>too many tokens (cost + context window pressure)</li> <li>repetition (same &ldquo;always include&rdquo; stuff every call)</li> <li>low signal-to-noise (agents skim; they don&rsquo;t &ldquo;feel&rdquo; what&rsquo;s important unless you structure it)</li> </ul> <p>Here are practical ways to &ldquo;compress&rdquo; without losing meaning&mdash;and still keep it usable as context.</p> <p><strong>1) Return a structured &ldquo;context pack&rdquo; instead of one big markdown blob</strong></p> <p>Even if the content inside is markdown, wrap it in a predictable structure so the agent can use it:</p> <ul> <li>items[]: each item has id, title, type, priority, excerpt, content_markdown</li> <li>summary: a short synthesized &ldquo;rules at a glance&rdquo;</li> <li>token_budget_hint: what to do if it&rsquo;s too long (e.g., drop low priority items)</li> </ul> <p>This helps the agent decide what to read first and what to ignore.</p> <p><strong>2) Add a two-tier output: &ldquo;compressed&rdquo; + &ldquo;full&rdquo;</strong></p> <p>For guidelines especially, you usually want:</p> <p><em>Tier A: compressed &ldquo;rule sheet&rdquo; (fast to read)</em></p> <ul> <li>10&ndash;30 bullet rules max</li> <li>grouped by category: Voice, Terms, Legal/Gov, Formatting, Accessibility, SEO, etc.</li> <li>include &ldquo;Do/Don&rsquo;t&rdquo; and examples only when they&rsquo;re truly essential</li> </ul> <p><em>Tier B: full markdown (only when needed)</em></p> <p>Return full markdown for the top N items, and for the rest return an excerpt + a pointer (&ldquo;expand if needed&rdquo;).</p> <p>You already have max_context_items; add a second knob like:</p> <ul> <li>verbosity: compressed | normal | full</li> </ul> <p>or</p> <ul> <li>include_full_text: boolean</li> </ul> <p>Default it to compressed for agents.</p> <p><strong>3) Compress within each item using a stable recipe</strong></p> <p>For brand/writing standards, the best compression isn&rsquo;t &ldquo;summarize everything&rdquo; &mdash; it&rsquo;s extract the actionable constraints.</p> <p>A great template per item:</p> <ul> <li>Purpose (1 line)</li> <li>Rules (bullets, imperative)</li> <li>Must / Must not (hard constraints)</li> <li>Preferred terms / Avoid terms</li> <li>Examples (only 1&ndash;2)</li> <li>When unsure (fallback rule)</li> </ul> <p>That turns a 2-page guideline into something an agent can reliably follow.</p> <p><strong>4) De-duplicate globally with a &ldquo;common rules&rdquo; section</strong></p> <p>If you have always-included items like &ldquo;Brand voice&rdquo; and &ldquo;Writing standards,&rdquo; they&rsquo;ll appear constantly.</p> <p>Instead:</p> <ul> <li>create a Common Rules section once at the top (compressed)</li> <li>then for each item, include only the delta (&ldquo;what&rsquo;s unique here&rdquo;)</li> </ul> <p>This is huge for token savings.</p> <p><strong>5) Add &ldquo;compression metadata&rdquo; so you can tune it later</strong></p> <p>Even if the agent only needs markdown, your selector should return metadata for ranking + trimming:</p> <ul> <li>importance (hard/soft)</li> <li>scope (global | per-agent | per-content-type | per-entity)</li> <li>freshness (last updated)</li> <li>length_chars or approximate token estimate</li> </ul> <p>Then you can enforce budgets:</p> <ul> <li>always include all importance=hard</li> <li>fit as many soft items as possible under a token cap</li> </ul> <p><strong>6) Consider a dedicated &ldquo;Guideline Digest&rdquo; cache</strong></p> <p>Because these are stable documents, you can precompute:</p> <ul> <li>a compressed digest per item</li> <li>a compressed digest per pool/agent</li> </ul> <p>Then runtime tool calls return the digest instantly, and only fetch full text when asked.</p> <p>Practical: store digest in the entity (field) or computed storage + cache tags.</p> <p><strong>7) A simple, agent-friendly markdown format</strong></p> <p>If you want to keep it markdown-only, I&rsquo;d output like this:</p> <pre>## Context Pack (compressed)<br>**Task:** &lt;task&gt;<br>**Selected items:** 12, 44, 51<br><br>### Rules to follow (highest priority)<br>- Must: ...<br>- Must not: ...<br>- Prefer: ...<br>- Avoid: ...<br><br>### Item 12 &mdash; Brand Voice (hard)<br>- Summary: ...<br>- Do: ...<br>- Don&rsquo;t: ...<br>- Preferred terms: ...<br>- Avoid terms: ...<br><br>### Item 44 &mdash; Accessibility &amp; Inclusive language (hard)<br>- Summary: ...<br>...</pre><p>That&rsquo;s still &ldquo;markdown,&rdquo; but it&rsquo;s compressed and scannable.</p> <p><strong>8) One immediate code-level tweak you can do without changing architecture</strong></p> <p>Right now you&rsquo;re building:</p> <p><code>"Selected IDs ...\n\n" . $result['text']</code></p> <p>Instead, generate:</p> <ul> <li>a short header</li> <li>then bulletized, categorized content</li> <li>limit each item to a max character budget unless verbosity=full</li> </ul> <p>Even crude limits help a lot.</p> <p>What I would implement first (smallest change, biggest win)</p> <p>Add an optional input: verbosity (compressed default)</p> <p>Return:</p> <ul> <li>a short &ldquo;Rules to follow&rdquo; section (global digest)</li> <li>per-item &ldquo;Summary + Key Rules&rdquo; only</li> <li>include full markdown only when verbosity=full</li> </ul> <p>If you paste a sample of what $result['text'] looks like today (even one item), I can rewrite the output format into a compressed version that still preserves the important constraints.</p> <h3 id="summary-ai-usage">AI usage (if applicable)</h3> <p>[x] AI Assisted Issue<br> This issue was generated with AI assistance, but was reviewed and refined by the creator.</p> <p>[ ] AI Assisted Code<br> This code was mainly generated by a human, with AI autocompleting or parts AI generated, but under full human supervision.</p> <p>[ ] AI Generated Code<br> This code was mainly generated by an AI with human guidance, and reviewed, tested, and refined by a human.</p> <p>[ ] Vibe Coded<br> This code was generated by an AI and has only been functionally tested.</p>
issue