Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions src/oss/langchain/context-engineering.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ import { tool } from "@langchain/core/tools";
import { z } from "zod";

const searchOrders = tool(
async ({ userId, status, limit = 10 }) => {
async ({ userId, status, limit }) => {
// Implementation here
},
{
Expand Down Expand Up @@ -1612,11 +1612,10 @@ Most real-world tools need more than just the LLM's parameters. They need user I

```typescript
import * as z from "zod";
import { tool } from "@langchain/core/tools";
import { createAgent } from "langchain";
import { createAgent, tool, type ToolRuntime } from "langchain";

const checkAuthentication = tool(
async (_, { runtime }) => {
async (_, runtime: ToolRuntime) => {
// Read from State: check current auth status
const currentState = runtime.state;
const isAuthenticated = currentState.authenticated || false;
Expand Down Expand Up @@ -1683,15 +1682,14 @@ Most real-world tools need more than just the LLM's parameters. They need user I

```typescript
import * as z from "zod";
import { tool } from "@langchain/core/tools";
import { createAgent } from "langchain";
import { createAgent, tool, type ToolRuntime } from "langchain";

const contextSchema = z.object({
userId: z.string(),
});

const getPreference = tool(
async ({ preferenceKey }, { runtime }) => {
async ({ preferenceKey }, runtime: ToolRuntime) => {
const userId = runtime.context.userId;

// Read from Store: get existing preferences
Expand Down Expand Up @@ -1781,7 +1779,7 @@ Most real-world tools need more than just the LLM's parameters. They need user I
});

const fetchUserData = tool(
async ({ query }, { runtime }) => {
async ({ query }, runtime: ToolRuntime<any, typeof contextSchema>) => {
// Read from Runtime Context: get API key and DB connection
const { userId, apiKey, dbConnection } = runtime.context;

Expand Down Expand Up @@ -1856,7 +1854,7 @@ and update the memory of the agent to make important context available to future
import { Command } from "@langchain/langgraph";

const authenticateUser = tool(
async ({ password }, { runtime }) => {
async ({ password }) => {
// Perform authentication
if (password === "correct") {
// Write to State: mark as authenticated using Command
Expand Down Expand Up @@ -1929,15 +1927,10 @@ and update the memory of the agent to make important context available to future

```typescript
import * as z from "zod";
import { tool } from "@langchain/core/tools";
import { createAgent } from "langchain";

const contextSchema = z.object({
userId: z.string(),
});
import { createAgent, tool, type ToolRuntime } from "langchain";

const savePreference = tool(
async ({ preferenceKey, preferenceValue }, { runtime }) => {
async ({ preferenceKey, preferenceValue }, runtime: ToolRuntime<any, typeof contextSchema>) => {
const userId = runtime.context.userId;

// Read existing preferences
Expand Down