DEV Community

Manan Sharma
Manan Sharma

Posted on

Custom prompt generator for cxgrd

As I planned in my previous post , I recently built the deterministic prompt generator for cxgrd.

How I built it

Just two steps:
1) Get the blast radius results from the subgraph

export interface PromptSubgraph {
  changeDescription: string;
  seedFiles: string[];
  affectedFiles: Array<Pick<ImpactedFile,
    'path' | 'severity' | 'reason' | 'distance' | 'impactType' | 'changeRequired' | 'suggestedFix'
  >>;
  dependencies: Array<{ from: string; to: string; type: string }>;
  symbols: Record<string, string[]>;
  architectureLayers: Record<string, string[]>;
  riskLevel: string;
  recommendations: string[];
}
Enter fullscreen mode Exit fullscreen mode

2) Embed these results in the prompt

for (const file of subgraph.affectedFiles) {
      if (subgraph.seedFiles.includes(file.path)) continue;
      const distance = file.distance === 1 ? 'direct dependent' : `transitive dependent, depth ${file.distance}`;
      const required = file.changeRequired ? 'review and update if the change requires it' : 'review for compatibility';
      const layers = layerByFile.get(file.path);
      const layerNote = layers?.length ? `; architecture layer: ${layers.join(', ')}` : '';
      lines.push(
        `- \`${file.path}\` — ${file.severity} risk, ${distance}; ${file.reason}; ${required}${layerNote}.`,
      );
      if (file.suggestedFix) lines.push(`  Suggested action: ${file.suggestedFix}`);
    }

[truncated]
Enter fullscreen mode Exit fullscreen mode

It can't interpret the intent as clearly as an AI model, but it can definitely guide your agent's focus on important modules, so it won't break your project.

And this custom generator will also cut API costs for me.

Try cxgrd here. I would love to hear what your think about this, any form of feedback/suggestion is welcomed.

Github : https://github.com/cxgrd/cli

Top comments (4)

Collapse
 
mattmoore_tech profile image
Matthew J. Moore •

Nice, I like that it's deterministic. Same change in, same prompt out, so when the agent does something weird you can actually tell whether it was the prompt or the model.

One suggestion: on a bigger repo the affected files list could get long fast, especially with transitive dependents. Sorting by severity and distance and capping it (say the top 15, then "plus N lower-risk files") would probably keep the agent focused and save even more on tokens. Direct dependents with changeRequired feel like the only ones it really needs every time.

Have you tried it on a change that touches a shared utils file? Curious how big the prompt gets there.

Collapse
 
geekgod382 profile image
Manan Sharma •

Thanks for your suggestion Matthew. I will try out sorting the files by severity.

If the request identifies a shared utility file, the generated prompt treats it as a direct change target and lists its detected dependents as files to review. For example, it might include:

src/utils/format.ts — direct change target identified from the request.
src/services/report.ts — high risk, direct dependent; Depends on format.ts; review and update if the change requires it.
Blast-radius summary: high overall risk; 1 direct target, 4 affected dependents.
Enter fullscreen mode Exit fullscreen mode

The prompt also includes relevant dependency edges, symbols, analyzer recommendations, and steps to test affected modules.

Collapse
 
mattmoore_tech profile image
Matthew J. Moore •

Nice, that output looks clean. Listing the utils file as the direct target and then its dependents by risk is exactly what I'd want to see. Looking forward to the version with sorting.

Collapse
 
ai_adam profile image
ai_adam •

Deterministic prompt generation là hướng đi rất đúng khi muốn reproducibility cho code review automation. Với cxgrd, việc tách biệt prompt logic ra generator riêng giúp:

  1. Version control cho prompt — diff được thay đổi prompt như code bình thường, không mất context khi rollback
  2. Testable — có thể unit test từng template, inject fixture data, verify output structure trước khi feed vào LLM
  3. Composable — chain multiple generators cho multi-pass review (style → security → architecture) mà không duplicate logic

Một điểm cần lưu ý: deterministic không có nghĩa là rigid. Cân nhắc thêm prompt parameterization (ví dụ: severity threshold, language-specific rules, team conventions) để generator adapt được cho nhiều repo/context khác nhau mà vẫn giữ reproducibility.

Có thử benchmark token usage so với approach dynamic prompt chưa? Việc fixed template có thể tốn token hơn ở chỗ include toàn bộ context, nhưng trade-off lại là predictable cost và easier caching PS: the tool I meant is on labagent .tech