Skip to content

Prevent Unnecessary Searches

Claude Desktop tends to search automatically even when the user can provide the location directly:

User: "Modify the ProcessData function in main.go"
Agent: "Let me search where ProcessData is defined..."
[Runs search_files unnecessarily]

Result: Wasted tokens and time when the user already knows the location.


Before running any search, ask the user if they know the location:

User: "Modify the ProcessData function"
Agent: "Do you know what file ProcessData is in? If not, I can search for it."
User: "main.go, around lines 150-180"
Agent: [Reads those lines directly and edits]

User: "Modify ProcessData in main.go"
Agent: "Do you know approximately which line? If not, I can search for it."

Case 2: User mentions a function but not the file

Section titled “Case 2: User mentions a function but not the file”
User: "Modify the ProcessData function"
Agent: "Which file is ProcessData in?"
User: "main.go"
Agent: "Do you know the line number? If not, I can search for it."
User: "I don't know where ProcessData is, but I need to modify it"
Agent: [Runs search_files]
User: "/home/user/project/src/main.go line 150"
Agent: [Reads directly, does not search]

Add this to your system prompt or custom instructions:

IMPORTANT: Avoid Unnecessary Searches
BEFORE executing any search (search_files, etc.):
1. Check if the user already provided the file path
2. Check if the user provided line numbers
3. If unsure, ask first: "Do you know where X is located?"
ONLY search if:
- The user explicitly says they don't know where it is
- The user asks to "find" or "search for" something
- The user clearly does not have the information
Benefit: Avoids sending unnecessary search results when the user already knows the location.

When the user already knows the location, skipping the search avoids sending the search results back to the model. The savings are largest when the search would return many files or large files. When the user does not know the location, the search is still necessary, so there is no savings.


If you are building your own agent, add this logic:

def before_search(query: str, context: dict):
"""Check if search is necessary before executing"""
user_message = context.get("user_message", "")
# Check if user provided an explicit path
if has_file_path(user_message):
return {"skip_search": True, "reason": "User provided path"}
# Check if user provided line numbers
if has_line_numbers(user_message):
return {"skip_search": True, "reason": "User provided lines"}
# Ask before searching
if is_exploratory(query):
return {"should_ask_first": True}
return {"proceed_with_search": True}

Being specific in your request eliminates unnecessary searches:

Vague (triggers search):

"Modify the ProcessData function"

Specific (no search needed):

"In main.go line 150, modify ProcessData to use async"

Semi-specific (reduced search):

"In main.go, modify ProcessData (search for the line if you can't see it)"

User: "In core/engine.go line 245, change timeout from 30 to 60"
Agent: [Reads line 245, edits directly]
Tokens: ~300
User: "Change the timeout in engine.go"
Agent: "Do you know which line the timeout is on?"
User: "No"
Agent: [Searches "timeout" in engine.go, finds line 245]
Tokens: ~1,200

User: "Modify X"
Agent: "Let me search for X..." [unnecessary]

Fix: Ask first if the user knows the location.

Section titled “Mistake 2: Asking when the user explicitly requested a search”
User: "Search for all functions that use 'timeout'"
Agent: "Which file?" [unnecessary]

Fix: If the user explicitly requests a search, run it directly.

Mistake 3: Ignoring context from earlier in the conversation

Section titled “Mistake 3: Ignoring context from earlier in the conversation”
User: "Now modify that function" [reference to previous message]
Agent: [Searches again] [unnecessary]

Fix: Use the conversation context already available.


In one sentence: Ask the user if they know the location before searching automatically.

Three steps:

  1. Before search_files, ask: “Do you know where X is?”
  2. Only search if the user says “I don’t know” or explicitly requests a search.
  3. This avoids unnecessary search results and their tokens.

Version: 4.5.29 Date: 2026-07-11