An agent with hundreds of tools cannot put every description in the prompt: it would burn the context window and confuse the model. The scalable approach is tool retrieval: embed the user's request and each tool's description, rank the tools by similarity and show the model only the top few. Even without a neural embedding, classic TF-IDF gives a strong baseline. Words that appear in few tool descriptions ("invoice", "weather") carry more weight than words that appear in all of them ("get", "data"), and the cosine similarity between the weighted query and description vectors ranks the tools.
Implement rank_tools.
tools maps tool names to description strings. Tokenize with re.findall(r'[a-z0-9]+', text.lower()).idf(w) = log((N + 1) / (df(w) + 1)) + 1 where N is the number of tools and df(w) the number of descriptions containing w. Words absent from every description still get df = 0.idf. Score each tool by the cosine similarity between the query vector and the description vector (0.0 if either has zero norm).top_k highest scoring tools, sorted by score descending and then by name ascending.Build one shared vocabulary from the query and all descriptions.
Compute df once from the descriptions, then reuse it for the query.
No results yet
Run your code to check it against the examples, or submit to run every test.