docs: cross link working w/ memory in tools (#4464)

This commit is contained in:
Vadym Barda
2025-04-30 13:52:10 -04:00
committed by GitHub
parent aa9651910c
commit ce2ba47aff
4 changed files with 116 additions and 70 deletions
+5 -65
View File
@@ -107,7 +107,7 @@ Common use cases:
config: RunnableConfig,
) -> list[AnyMessage]:
# highlight-next-line
user_name = config.get("configurable", {}).get("user_name")
user_name = config["configurable"].get("user_name")
system_msg = f"You are a helpful assistant. User's name is {user_name}"
return [{"role": "system", "content": system_msg}] + state["messages"]
@@ -162,7 +162,7 @@ Common use cases:
})
```
## Tools
## Accessing Context in Tools
Tools can access context through special parameter **annotations**.
@@ -183,7 +183,7 @@ Tools can access context through special parameter **annotations**.
) -> str:
"""Look up user info."""
# highlight-next-line
user_id = config.get("configurable", {}).get("user_id")
user_id = config["configurable"].get("user_id")
return "User is John Smith" if user_id == "user_123" else "Unknown user"
agent = create_react_agent(
@@ -231,66 +231,6 @@ Tools can access context through special parameter **annotations**.
})
```
### Update Context from Tools
## Update context from tools
Tools can modify the agent's state during execution. This is useful for persisting intermediate results or making information accessible to subsequent tools or prompts.
```python
from typing import Annotated
from langchain_core.tools import InjectedToolCallId
from langchain_core.messages import ToolMessage
from langgraph.prebuilt import InjectedState
from langgraph.types import Command
class CustomState(AgentState):
# highlight-next-line
user_name: str
def get_user_info(
# highlight-next-line
tool_call_id: Annotated[str, InjectedToolCallId],
# highlight-next-line
config: RunnableConfig
) -> Command:
"""Look up user info."""
# highlight-next-line
user_id = config.get("configurable", {}).get("user_id")
name = "John Smith" if user_id == "user_123" else "Unknown user"
return Command(update={
# highlight-next-line
"user_name": name,
# update the message history
# highlight-next-line
"messages": [
ToolMessage(
"Successfully looked up user information",
# highlight-next-line
tool_call_id=tool_call_id
)
]
})
def greet(
# highlight-next-line
state: Annotated[CustomState, InjectedState]
) -> str:
"""Use this to greet the user once you found their info."""
user_name = state["user_name"]
return f"Hello {user_name}!"
agent = create_react_agent(
model="anthropic:claude-3-7-sonnet-latest",
tools=[get_user_info, greet],
# highlight-next-line
state_schema=CustomState
)
agent.invoke(
{"messages": [{"role": "user", "content": "greet the user"}]},
# highlight-next-line
config={"configurable": {"user_id": "user_123"}}
)
```
For more details, see [how to update state from tools](../how-tos/update-state-from-tools.ipynb).
Tools can update agent's context (state and long-term memory) during execution. This is useful for persisting intermediate results or making information accessible to subsequent tools or prompts. See [Memory](./memory.md#read-short-term) guide for more information.