You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/oss/langchain/agents.mdx
+60Lines changed: 60 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -464,10 +464,70 @@ const agent = createAgent({
464
464
465
465
:::python
466
466
When no @[`system_prompt`] is provided, the agent will infer its task from the messages directly.
467
+
468
+
The @[`system_prompt`] parameter accepts either a `str` or a @[`SystemMessage`]. Using a `SystemMessage` gives you more control over the prompt structure, which is useful for provider-specific features like [Anthropic's prompt caching](/oss/integrations/chat/anthropic#prompt-caching):
469
+
470
+
```python wrap
471
+
from langchain.agents import create_agent
472
+
from langchain.messages import SystemMessage, HumanMessage
473
+
474
+
literary_agent = create_agent(
475
+
model="anthropic:claude-sonnet-4-5",
476
+
system_prompt=SystemMessage(
477
+
content=[
478
+
{
479
+
"type": "text",
480
+
"text": "You are an AI assistant tasked with analyzing literary works.",
481
+
},
482
+
{
483
+
"type": "text",
484
+
"text": "<the entire contents of 'Pride and Prejudice'>",
485
+
"cache_control": {"type": "ephemeral"}
486
+
}
487
+
]
488
+
)
489
+
)
490
+
491
+
result = literary_agent.invoke(
492
+
{"messages": [HumanMessage("Analyze the major themes in 'Pride and Prejudice'.")]}
493
+
)
494
+
```
495
+
496
+
The `cache_control` field with `{"type": "ephemeral"}` tells Anthropic to cache that content block, reducing latency and costs for repeated requests that use the same system prompt.
467
497
:::
468
498
469
499
:::js
470
500
When no `systemPrompt` is provided, the agent will infer its task from the messages directly.
501
+
502
+
The `systemPrompt` parameter accepts either a `string` or a `SystemMessage`. Using a `SystemMessage` gives you more control over the prompt structure, which is useful for provider-specific features like [Anthropic's prompt caching](/oss/integrations/chat/anthropic#prompt-caching):
text: "You are an AI assistant tasked with analyzing literary works.",
515
+
},
516
+
{
517
+
type: "text",
518
+
text: "<the entire contents of 'Pride and Prejudice'>",
519
+
cache_control: { type: "ephemeral" }
520
+
}
521
+
]
522
+
})
523
+
});
524
+
525
+
const result =awaitliteraryAgent.invoke({
526
+
messages: [newHumanMessage("Analyze the major themes in 'Pride and Prejudice'.")]
527
+
});
528
+
```
529
+
530
+
The `cache_control` field with `{ type: "ephemeral" }` tells Anthropic to cache that content block, reducing latency and costs for repeated requests that use the same system prompt.
0 commit comments