Skip to content

Commit 668b0f0

Browse files
minor adjustments
1 parent b9d2668 commit 668b0f0

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/oss/langchain/agents.mdx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ Model instances give you complete control over configuration. Use them when you
128128

129129
#### Dynamic model
130130

131-
:::python
132-
133131
Dynamic models are selected at <Tooltip tip="The execution environment of your agent, containing immutable configuration and contextual data that persists throughout the agent's execution (e.g., user IDs, session details, or application-specific configuration).">runtime</Tooltip> based on the current <Tooltip tip="The data that flows through your agent's execution, including messages, custom fields, and any information that needs to be tracked and potentially modified during processing (e.g., user preferences or tool usage stats).">state</Tooltip> and context. This enables sophisticated routing logic and cost optimization.
134132

133+
:::python
134+
135135
To use a dynamic model, you need to provide a function that receives the graph state and runtime and returns an instance of `BaseChatModel` with the tools bound to it using `.bind_tools(tools)`, where `tools` is a subset of the `tools` parameter.
136136

137137
```python
@@ -153,11 +153,6 @@ agent = create_agent(select_model, tools=tools)
153153
```
154154
:::
155155
:::js
156-
<Info>
157-
**`state`**: The data that flows through your agent's execution, including messages, custom fields, and any information that needs to be tracked and potentially modified during processing (e.g. user preferences or tool usage stats).
158-
</Info>
159-
160-
Dynamic models are selected at runtime based on the current state and context. This enables sophisticated routing logic and cost optimization.
161156

162157
To use a dynamic model, you need to provide a function that receives the graph state and runtime and returns an instance of `BaseChatModel` with the tools bound to it using `.bindTools(tools)`, where `tools` is a subset of the `tools` parameter.
163158

@@ -467,7 +462,12 @@ When no `prompt` is provided, the agent will infer its task from the messages di
467462

468463
#### Dynamic prompts with middleware
469464

465+
:::python
470466
For more advanced use cases where you need to modify the system prompt based on runtime context or agent state, you can use the `modify_model_request` decorator to create a simple custom middleware.
467+
:::
468+
:::js
469+
For more advanced use cases where you need to modify the system prompt based on runtime context or agent state, you can use the `modifyModelRequest` decorator to create a simple custom middleware.
470+
:::
471471

472472
Dynamic system prompt is especially useful for personalizing prompts based on user roles, conversation context, or other changing factors:
473473

@@ -522,7 +522,7 @@ const contextSchema = z.object({
522522

523523
const agent = createAgent({
524524
model: "openai:gpt-4o",
525-
tools: [...],
525+
tools: [/* ... */],
526526
contextSchema,
527527
middleware: [
528528
dynamicSystemPromptMiddleware<z.infer<typeof contextSchema>>((state, runtime) => {

src/oss/langchain/middleware.mdx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,11 +470,11 @@ const result = await agent.invoke({ messages: [HumanMessage("What's my name?")]
470470

471471
### Dynamic system prompt
472472

473+
:::python
473474
A system prompt can be dynamically set right before each model invocation using the `@modify_model_request` decorator. This middleware is particularly useful when the prompt depends on the current agent state or runtime context.
474475

475476
For example, you can adjust the system prompt based on the user's expertise level:
476477

477-
:::python
478478
```python
479479
from typing import TypedDict
480480

@@ -513,8 +513,12 @@ result = agent.invoke(
513513
)
514514
```
515515
:::
516-
517516
:::js
517+
518+
A system prompt can be dynamically set right before each model invocation using the `dynamicSystemPromptMiddleware` middleware. This middleware is particularly useful when the prompt depends on the current agent state or runtime context.
519+
520+
For example, you can adjust the system prompt based on the user's expertise level:
521+
518522
```typescript
519523
import { z } from "zod";
520524
import { createAgent } from "langchain";

0 commit comments

Comments
 (0)