
Intelligent Tools: Context-Aware Execution
Give your tools a memory. Learn how to design tools that adjust their behavior based on the current state, user history, and orchestrator metadata.
Context-Aware Tools
Most tools are "Static"—they take an input and return an output regardless of who is asking. However, a production agent needs Context-Aware Tools.
- A
search_docstool should know if the user is aDeveloperor aManager. - A
get_balancetool should automatically know whichaccount_idbelongs to the current thread.
In this lesson, we will learn how to "Secretly" pass state into our tools so the agent doesn't have to worry about the plumbing.
1. The Hidden Parameter Pattern
When an LLM calls a tool, it only provides the parameters defined in the schema (e.g., query). It doesn't know about user_id or session_token.
We use Injected Parameters to bridge this gap.
The Implementation
- The Orchestrator (LangGraph) sees a tool call.
- Before executing the tool, it "Enriches" the arguments with data from the State.
# The LLM thinks the tool only takes 'query'
# But the Python function also takes 'authenticated_user_id'
def search_private_docs(query: str, state_user_id: str):
# This logic ensures the agent can't search other users' files
return db.search(query, owner=state_user_id)
2. Tools that "Read" the Chat History
Sometimes, a tool needs to know what was said 5 minutes ago to be accurate.
Example: The "Clarification" Tool
Say an agent has a tool execute_sql.
If the tool sees that the user previously said "Show me ONLY North American sales," the tool can automatically add WHERE region = 'NA' to every query the agent tries to run, even if the agent forgot to include it.
3. The "Dynamic Schema" Strategy
You can change what tools the agent sees based on the state.
| State Condition | Tool List |
|---|---|
| User is Unauthenticated | login, search_public_faqs |
| User is Admin | get_user_logs, delete_database, reset_password |
| Task is in "Drafting" mode | write_snippet, search_vocabulary |
Why? By limiting the toolset dynamically, you reduce the model's confusion and prevent unauthorized tool access.
4. Context-Aware Error Messages
A context-aware tool doesn't just return a generic error. It looks at the goal.
- Generic:
Error: Permission Denied. - Context-Aware:
Error: You are trying to access the 'CEO_Bonuses' table. Your current user (John) only has 'Staff' level access. Please try searching for 'Standard_Salaries' instead.
5. Implementation: LangChain RunnableConfig
In LangChain, we use the config object to pass context through the system without the LLM seeing it.
from langchain_core.runnables import RunnableConfig
async def my_tool(query: str, config: RunnableConfig):
# Retrieve data injected by the API layer (Module 10.3)
user_id = config.get("configurable", {}).get("user_id")
return await db.lookup(query, user_id)
6. The "Environment Aware" Tool
A tool can adjust its behavior based on the Time or Location.
search_newstool → automatically appendspublished_after: [Today's Date].book_flighttool → automatically sets theorigin_cityto the user's current GPS location from the state.
Summary and Mental Model
Think of a Context-Aware Tool like a Smart Assistant.
- A Dumb Assistant asks: "Who should I call?" (Requires explicit input).
- A Smart Assistant sees you are in a meeting with John and says: "I see you're with John, should I call his office?" (Uses context).
The more context your tools have, the less work the LLM has to do.
Exercise: Context Injection
- The Scenario: You are building an agent for a Law Firm.
- The
search_contractstool must only return documents for the specificclient_idstored in the LangGraph state. - Write the Python function signature for this tool, identifying which parameter is for the LLM and which is for the State.
- The
- Security: Why is it safer to inject the
user_idfrom the Config rather than having the agent provide it as a schema parameter?- (Hint: Could an agent hallucinate the wrong
user_id?)
- (Hint: Could an agent hallucinate the wrong
- Logic: If an agent is in "Help Mode," how would you change the
Searchtool to return "Simple Explanations" instead of "Raw Code"? Ready to manage hundreds of tools? Let's move to High-Cardinality Tool Sets.