Skip to content
Merged
Show file tree
Hide file tree
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
65 changes: 62 additions & 3 deletions libs/providers/langchain-anthropic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,65 @@ const response = await model.stream({
});
```

## Tools

This package provides LangChain-compatible wrappers for Anthropic's built-in tools. These tools can be bound to `ChatAnthropic` using `bindTools()` or any [`ReactAgent`](https://docs.langchain.com/oss/javascript/langchain/agents).

### Memory Tool

The memory tool (`memory_20250818`) enables Claude to store and retrieve information across conversations through a memory file directory. Claude can create, read, update, and delete files that persist between sessions, allowing it to build knowledge over time without keeping everything in the context window.

```typescript
import { ChatAnthropic, tools } from "@langchain/anthropic";

// Create a simple in-memory file store (or use your own persistence layer)
const files = new Map<string, string>();

const memory = tools.memory_20250818({
execute: async (command) => {
switch (command.command) {
case "view":
if (!command.path || command.path === "/") {
return Array.from(files.keys()).join("\n") || "Directory is empty.";
}
return (
files.get(command.path) ?? `Error: File not found: ${command.path}`
);
case "create":
files.set(command.path!, command.file_text ?? "");
return `Successfully created file: ${command.path}`;
case "str_replace":
const content = files.get(command.path!);
if (content && command.old_str) {
files.set(
command.path!,
content.replace(command.old_str, command.new_str ?? "")
);
}
return `Successfully replaced text in: ${command.path}`;
case "delete":
files.delete(command.path!);
return `Successfully deleted: ${command.path}`;
// Handle other commands: insert, rename
default:
return `Unknown command`;
}
},
});

const llm = new ChatAnthropic({
model: "claude-sonnet-4-5-20250929",
});

const llmWithMemory = llm.bindTools([memory]);

const response = await llmWithMemory.invoke(
"Remember that my favorite programming language is TypeScript"
);
```

For more information, see [Anthropic's Memory Tool documentation](https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/memory-tool).

## Development

To develop the Anthropic package, you'll need to follow these instructions:
Expand Down Expand Up @@ -103,8 +162,8 @@ Test files should live within a `tests/` file in the `src/` folder. Unit tests s
end in `.int.test.ts`:

```bash
$ pnpm test
$ pnpm test:int
pnpm test
pnpm test:int
```

### Lint & Format
Expand All @@ -124,5 +183,5 @@ If you add a new file to be exported, either import & re-export from `src/index.
After running `pnpm build`, publish a new version with:

```bash
$ npm publish
npm publish
```
4 changes: 2 additions & 2 deletions libs/providers/langchain-anthropic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"vectorstores"
],
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.cts",
"exports": {
".": {
Expand All @@ -94,6 +95,5 @@
"CHANGELOG.md",
"README.md",
"LICENSE"
],
"module": "./dist/index.js"
]
}
1 change: 1 addition & 0 deletions libs/providers/langchain-anthropic/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./chat_models.js";
export { convertPromptToAnthropic } from "./utils/prompts.js";
export { type ChatAnthropicContentBlock } from "./types.js";
export * from "./tools/index.js";
2 changes: 2 additions & 0 deletions libs/providers/langchain-anthropic/src/tools/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { memory_20250818 } from "./memory.js";
export type * from "./types.js";
78 changes: 78 additions & 0 deletions libs/providers/langchain-anthropic/src/tools/memory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { tool } from "@langchain/core/tools";
import type { DynamicStructuredTool, ToolRuntime } from "@langchain/core/tools";

import type { MemoryTool20250818Options } from "./types.js";

/**
* Creates an Anthropic memory tool that can be used with ChatAnthropic.
*
* The memory tool enables Claude to store and retrieve information across conversations
* through a memory file directory. Claude can create, read, update, and delete files that
* persist between sessions, allowing it to build knowledge over time without keeping
* everything in the context window.
*
* @example
* ```typescript
* import { ChatAnthropic, memory_20250818 } from "@langchain/anthropic";
*
* const llm = new ChatAnthropic({
* model: "claude-sonnet-4-5-20250929"
* });
*
* const memory = memory_20250818({
* execute: async (args) => {
* // handle memory command execution
* // ...
* },
* });
* const llmWithMemory = llm.bindTools([memory]);
*
* const response = await llmWithMemory.invoke("Remember that I like Python");
* ```
*
* @param options - Optional configuration for the memory tool (currently unused)
* @param options.execute - Optional execute function that handles memory command execution.
* @returns The memory tool object that can be passed to `bindTools`
*
* @see https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/memory-tool
*/
export function memory_20250818(
options?: MemoryTool20250818Options
): DynamicStructuredTool {
const memoryTool = tool(
options?.execute as (
input: unknown,
runtime: ToolRuntime<unknown, unknown>
) => string | Promise<string>,
{
name: "memory",
schema: {
type: "object",
properties: {
command: {
type: "string",
enum: [
"view",
"create",
"str_replace",
"insert",
"delete",
"rename",
],
},
},
required: ["command"],
},
}
);

memoryTool.extras = {
...(memoryTool.extras ?? {}),
providerToolDefinition: {
type: "memory_20250818",
name: "memory",
},
};

return memoryTool;
}
Loading
Loading