Files
docs/build/snippets/python/code-samples/context-engineering-runtime-context-js.mdx
T
2026-07-29 10:28:19 +00:00

247 lines
7.1 KiB
Plaintext

<CodeGroup>
```ts Google
import { createDeepAgent } from "deepagents";
import { tool } from "langchain";
import type { ToolRuntime } from "@langchain/core/tools";
import * as z from "zod";
const contextSchema = z.object({
userId: z.string(),
apiKey: z.string(),
});
const fetchUserData = tool(
async (input, runtime: ToolRuntime<unknown, typeof contextSchema>) => {
const userId = runtime.context?.userId;
return `Data for user ${userId}: ${input.query}`;
},
{
name: "fetch_user_data",
description: "Fetch data for the current user",
schema: z.object({ query: z.string() }),
},
);
const agent = await createDeepAgent({
model: "google-genai:gemini-3.6-flash",
tools: [fetchUserData],
contextSchema,
});
const result = await agent.invoke(
{ messages: [{ role: "user", content: "Get my recent activity" }] },
{ context: { userId: "user-123", apiKey: "sk-..." } },
);
```
```ts OpenAI
import { createDeepAgent } from "deepagents";
import { tool } from "langchain";
import type { ToolRuntime } from "@langchain/core/tools";
import * as z from "zod";
const contextSchema = z.object({
userId: z.string(),
apiKey: z.string(),
});
const fetchUserData = tool(
async (input, runtime: ToolRuntime<unknown, typeof contextSchema>) => {
const userId = runtime.context?.userId;
return `Data for user ${userId}: ${input.query}`;
},
{
name: "fetch_user_data",
description: "Fetch data for the current user",
schema: z.object({ query: z.string() }),
},
);
const agent = await createDeepAgent({
model: "openai:gpt-5.5",
tools: [fetchUserData],
contextSchema,
});
const result = await agent.invoke(
{ messages: [{ role: "user", content: "Get my recent activity" }] },
{ context: { userId: "user-123", apiKey: "sk-..." } },
);
```
```ts Anthropic
import { createDeepAgent } from "deepagents";
import { tool } from "langchain";
import type { ToolRuntime } from "@langchain/core/tools";
import * as z from "zod";
const contextSchema = z.object({
userId: z.string(),
apiKey: z.string(),
});
const fetchUserData = tool(
async (input, runtime: ToolRuntime<unknown, typeof contextSchema>) => {
const userId = runtime.context?.userId;
return `Data for user ${userId}: ${input.query}`;
},
{
name: "fetch_user_data",
description: "Fetch data for the current user",
schema: z.object({ query: z.string() }),
},
);
const agent = await createDeepAgent({
model: "anthropic:claude-sonnet-4-6",
tools: [fetchUserData],
contextSchema,
});
const result = await agent.invoke(
{ messages: [{ role: "user", content: "Get my recent activity" }] },
{ context: { userId: "user-123", apiKey: "sk-..." } },
);
```
```ts OpenRouter
import { createDeepAgent } from "deepagents";
import { tool } from "langchain";
import type { ToolRuntime } from "@langchain/core/tools";
import * as z from "zod";
const contextSchema = z.object({
userId: z.string(),
apiKey: z.string(),
});
const fetchUserData = tool(
async (input, runtime: ToolRuntime<unknown, typeof contextSchema>) => {
const userId = runtime.context?.userId;
return `Data for user ${userId}: ${input.query}`;
},
{
name: "fetch_user_data",
description: "Fetch data for the current user",
schema: z.object({ query: z.string() }),
},
);
const agent = await createDeepAgent({
model: "openrouter:openrouter:z-ai/glm-5.2",
tools: [fetchUserData],
contextSchema,
});
const result = await agent.invoke(
{ messages: [{ role: "user", content: "Get my recent activity" }] },
{ context: { userId: "user-123", apiKey: "sk-..." } },
);
```
```ts Fireworks
import { createDeepAgent } from "deepagents";
import { tool } from "langchain";
import type { ToolRuntime } from "@langchain/core/tools";
import * as z from "zod";
const contextSchema = z.object({
userId: z.string(),
apiKey: z.string(),
});
const fetchUserData = tool(
async (input, runtime: ToolRuntime<unknown, typeof contextSchema>) => {
const userId = runtime.context?.userId;
return `Data for user ${userId}: ${input.query}`;
},
{
name: "fetch_user_data",
description: "Fetch data for the current user",
schema: z.object({ query: z.string() }),
},
);
const agent = await createDeepAgent({
model: "fireworks:accounts/fireworks/models/glm-5p2",
tools: [fetchUserData],
contextSchema,
});
const result = await agent.invoke(
{ messages: [{ role: "user", content: "Get my recent activity" }] },
{ context: { userId: "user-123", apiKey: "sk-..." } },
);
```
```ts Baseten
import { createDeepAgent } from "deepagents";
import { tool } from "langchain";
import type { ToolRuntime } from "@langchain/core/tools";
import * as z from "zod";
const contextSchema = z.object({
userId: z.string(),
apiKey: z.string(),
});
const fetchUserData = tool(
async (input, runtime: ToolRuntime<unknown, typeof contextSchema>) => {
const userId = runtime.context?.userId;
return `Data for user ${userId}: ${input.query}`;
},
{
name: "fetch_user_data",
description: "Fetch data for the current user",
schema: z.object({ query: z.string() }),
},
);
const agent = await createDeepAgent({
model: "baseten:zai-org/GLM-5.2",
tools: [fetchUserData],
contextSchema,
});
const result = await agent.invoke(
{ messages: [{ role: "user", content: "Get my recent activity" }] },
{ context: { userId: "user-123", apiKey: "sk-..." } },
);
```
```ts Ollama
import { createDeepAgent } from "deepagents";
import { tool } from "langchain";
import type { ToolRuntime } from "@langchain/core/tools";
import * as z from "zod";
const contextSchema = z.object({
userId: z.string(),
apiKey: z.string(),
});
const fetchUserData = tool(
async (input, runtime: ToolRuntime<unknown, typeof contextSchema>) => {
const userId = runtime.context?.userId;
return `Data for user ${userId}: ${input.query}`;
},
{
name: "fetch_user_data",
description: "Fetch data for the current user",
schema: z.object({ query: z.string() }),
},
);
const agent = await createDeepAgent({
model: "ollama:north-mini-code-1.0",
tools: [fetchUserData],
contextSchema,
});
const result = await agent.invoke(
{ messages: [{ role: "user", content: "Get my recent activity" }] },
{ context: { userId: "user-123", apiKey: "sk-..." } },
);
```
</CodeGroup>