Skip to content
mentoor

Topic 2 of 5 · 4 min read

Search tools agents use to find context

Goal: Pick the lookup tool an agent should use for a given question and write a tool description that steers it there.

Give the agent a few sharp lookup tools, each with a description that says what it finds and when to use it, and the agent will search instead of guessing. The model chooses tools from their names and descriptions alone, so the description is part of your prompt.

Check yourself · 1 of 3

You know an error string, "ECONNRESET at pool.js", but not which file raises it. Which tool fits best?

Choose one answer

The usual toolbox

  • Glob finds files by name pattern (src/**/*.test.ts). Use it when you know the shape of the path.
  • Grep finds text inside files (a function name, an error string, an env var). Use it when you know a word, not a location.
  • File read opens a file, ideally a line range, once you know where to look.
  • Doc or vector search finds passages by meaning in a wiki, tickets or manuals. Use it when you do not know the right words.
  • Web search and fetch cover public, current information such as library release notes.
  • MCP resources and tools expose data from your own systems (database, issue tracker) through the Model Context Protocol. Resources are data an application or model can read, identified by URIs.
  • Light references are the glue: a path, a ticket id, a URL or a stored query that points to detail without carrying it.

Matching question to tool

"Where is MAX_RETRIES defined?" is grep. "What does the billing module do?" is glob, then read the entry file. "What did we decide about refunds?" is doc search. "Does the new SDK still have client.stream()?" is the vendor's docs, not memory.

Steering with the description

Weak:

{ "name": "search", "description": "Searches things." }

Better:

{
  "name": "search_docs",
  "description": "Semantic search over internal runbooks and design docs. Use for 'how do we...' or 'why did we...' questions. Returns up to 5 passages with doc ids. For exact identifiers or error strings, use grep_code instead.",
  "input_schema": {
    "type": "object",
    "properties": { "query": { "type": "string", "description": "A natural-language question" } },
    "required": ["query"]
  }
}

The better version says what it covers, when to use it, what it returns, and when not to use it.

What goes wrong

  • One vague "search" tool. The agent uses it for everything and gets noisy results.
  • Overlapping tools. Two tools that both "find files" make the model choose at random. If a human cannot say which to use, the model cannot either.
  • No way to narrow. A read tool that only returns whole files wastes the window; offer line ranges or a max length.
  • Hidden tools. A capability missing from the tool list will not be used. The agent guesses instead.

Examples

Routing four questions

  • "Where is parseConfig called?" -> grep for the symbol.
  • "List all migration files" -> glob db/migrations/*.sql.
  • "Why did we pick Postgres over Mongo?" -> doc search over design records.
  • "What changed in the payments SDK this month?" -> fetch the vendor's changelog.

Each tool answers one kind of question cheaply. Reading files at random answers none of them well.

Rewriting an ambiguous pair

Two tools, find and search, both described as "finds code". The agent alternates between them. Rename and redescribe: glob_files ("match file paths by pattern, returns paths only") and grep_code ("match text in file contents, returns file, line and a snippet"). Selection becomes predictable.

Key points

  • Use glob for names, grep for exact text, doc search for meaning, and web or vendor docs for current external facts.
  • The tool name and description are the only guidance the model has, so write them as part of the prompt.
  • State what the tool covers, when to use it, what it returns and when to use a different tool.
  • Avoid overlapping tools; distinct jobs make selection reliable.
  • Light references such as paths, ids and URLs let an agent fetch detail later instead of carrying it.

Common mistakes

  • Exposing one catch-all search tool with a one-line description.
  • Defining two tools with overlapping jobs and similar descriptions.
  • Returning whole files when a line range or snippet would do.
  • Relying on the model's memory for library APIs instead of giving it the docs to search.

Check yourself · 2 of 3

An agent keeps picking the wrong one of two tools whose descriptions both say "finds code". What is the best fix?

Choose one answer

Check yourself · 3 of 3

What is a light reference in an agent's context?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic