跪拜 Guibai
← All articles
Agent · AI Programming · LangChain

Stop Patching Agent Prompts: Route by Intent or You'll Break Working Features

By Sailing ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Treating a prompt edit as a text tweak is what causes silent regressions in production Agents. Intent-based routing plus a regression suite and versioned rollback is the only way to fix one case without breaking the ones that already worked.

Summary

Agent prompts in production grow as teams bolt on "must" and "never" clauses after each failure. A typical case: a user asks "What does log query support?" and the Agent treats it as a request to run a production log query, because the base rule routes on the keyword "log query" rather than intent. Three later patches each look right, but they all patch the wrong base rule, so routing becomes inconsistent and existing features break. The fix replaces that base rule with one that routes on the user's task and the data source they need: capability questions go to search_docs, explicit real-log queries go to query_logs, and incomplete requests trigger a follow-up. The main prompt holds routing principles and contrasting examples; tool descriptions explain each tool's purpose; a Zod schema constrains parameters. A regression table covers the broken case, the still-working real query, a near-miss non-query phrasing, and an under-specified request. Tests reuse real tool names and schemas but swap func for mocks, then assert on tool name and arguments from AIMessage.tool_calls. Prompt changes are treated as behavior releases: version the prompt, tests and a change note in Git, and record the deploy with commit, model, prompt, tools and eval set for rollback.

Takeaways
— A rule that routes on 'user mentioned logs' misclassifies questions about log capabilities as requests to query production logs.
— Appending 'don't call the tool' exceptions to a wrong base rule produces inconsistent routing; the fix is to replace the base rule with intent and data-source conditions.
— Split responsibilities: the main prompt handles routing and examples, the tool description explains what the tool is for, and the Zod schema constrains parameters.
— The query_logs tool must still enforce auth, environment permissions, time range limits, timeout, audit and result masking in its execution code, independent of the prompt.
— Regression tests should cover the fixed case, the previously working real-query case, a similar non-query phrasing, and an under-specified request that should trigger a follow-up question.
— When testing, keep the tool's name, description and schema but replace func with a mock; assert on AIMessage.tool_calls, not just 'called a tool'.
— Run key cases multiple times because model output varies; add new inputs to the test suite whenever the same problem reappears.
— Version the prompt, tests and a change note in Git, and record the deploy with git_commit, model, prompt, tools and eval_set so you can roll back to a previous passing snapshot.
Conclusions

A prompt change is effectively a behavior release, not a text edit; the article treats verification and rollback as part of the deliverable, which is exactly how code changes are managed.

The proposed fix depends on the model following a single coherent routing principle, and the regression suite exists because model output fluctuates, so a single passing run proves nothing stable.

Keeping tool descriptions and schemas separate from the main prompt reduces contradictory instructions, but it still requires updating both places in the same change, which the article explicitly calls out.

From the discussion
Featured comments
badhope

The line "replace conflicting rules first, never keep appending" is key. We also used to keep appending "don't..." at the end of the Prompt, which ended up suppressing old features. Later we changed to splitting routing by task type, and moved the tool parameter constraints to the schema, leaving only the judgment boundary in the Prompt. Are your regression test cases fixed at 4 types, or will you expand them based on tool call combinations?

Sailing

This article is relatively simple. In practice, what it comes down to is tool classification and tool combinations. The fixed 4 types basically won't change; it's simple to develop and not easy to get wrong.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗