Course contentsShow
← AI Engineering
Lesson 581 of 1,88614. Function Calling and Tool UseFree lesson

Limiting Available Tools by Context

Dynamically filter the tool set based on conversation state, user permissions, or task requirements.

Limiting Available Tools by Context

What you'll learn: How to dynamically filter which tools the LLM can call based on the current conversation state, user permissions, or task requirements.

Why Context Matters

Imagine handing someone your entire toolbox when they just need a screwdriver. Not only is it overwhelming, but they might grab the wrong tool entirely. The same principle applies to function calling: presenting every available tool to an LLM on every request wastes tokens, confuses the model, and can lead to incorrect tool selection.

The Core Concept

Instead of always passing the full set of available functions, you selectively filter which tools appear in each API request based on contextual factors:

  • Conversation state: If the user is browsing products, show search and filter tools—not checkout functions yet
  • User permissions: Administrative tools only appear for admin users, not regular customers
  • Task phase: During data collection, show input tools; during analysis, show computation tools
  • Previous actions: After a database query, offer visualization tools; before it, don't

This filtering happens before you make the LLM API call, in your application logic.

How It Works in Practice

Your application maintains context (user role, conversation phase, etc.), then filters your function registry accordingly:

if user_role == "admin":
    available_tools = all_tools
else:
    available_tools = [t for t in all_tools if not t.requires_admin]

response = llm.chat(messages, tools=available_tools)

You're essentially asking: "Given what I know right now, which subset of tools makes sense?"

Key Takeaway: Dynamically limiting tools based on context improves LLM accuracy, reduces token costs, and enforces security boundaries—making your function-calling system smarter and safer.