> For the complete documentation index, see [llms.txt](https://black-sheep-finance.gitbook.io/black-sheep-finance-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://black-sheep-finance.gitbook.io/black-sheep-finance-docs/guides/langchain.md).

# LangChain integration

Give a LangChain agent a real USDC wallet, gated by your policy. The LLM gets three tools: `send_usdc`, `get_wallet_info`, `list_recent_transactions`.

## Install

```bash
npm i @blacksheep/sdk langchain @langchain/openai @langchain/core zod
```

## Minimal example

```ts
import { ChatOpenAI } from "@langchain/openai";
import { AgentExecutor, createToolCallingAgent } from "langchain/agents";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { blackSheepTools } from "@blacksheep/sdk/langchain";

const tools = blackSheepTools({
  apiKey: process.env.BS_API_KEY!,
  agentId: process.env.BS_AGENT_ID!, // a wallet you issued in the dashboard
});

const prompt = ChatPromptTemplate.fromMessages([
  ["system",
   "You are a research assistant. When the user asks for paid data, " +
   "use send_usdc to pay the vendor. Confirm the recipient address before paying."],
  ["human", "{input}"],
  ["placeholder", "{agent_scratchpad}"],
]);

const agent = await createToolCallingAgent({
  llm: new ChatOpenAI({ model: "gpt-4o-mini", temperature: 0 }),
  tools,
  prompt,
});

const executor = new AgentExecutor({ agent, tools });

const result = await executor.invoke({
  input: "Pay 10 USDC to 7xKX...abc for the population dataset.",
});

console.log(result.output);
```

## What the LLM sees

* `send_usdc({ to, amount, memo? })` — returns `{ status: "sent", signature }` or `{ status: "pending_approval", approval_id }`.
* `get_wallet_info()` — returns label, pubkey, and current policy.
* `list_recent_transactions()` — last 100 across the account.

## Safety model

* The LLM **never** holds the private key.
* The LLM **cannot** raise its own limits — it has no `update_policy` tool by design.
* If the LLM hallucinates a recipient and the allowlist is set, the call is denied server-side.
* If it tries to spend over the approval threshold, the call returns `pending_approval` and waits for a human.

## Tips

* Set `temperature: 0` for any agent that spends.
* Keep `approval_threshold_usdc` low while you're tuning prompts.
* Log `executor.invoke` output to your own observability — combine with `tx.sent` webhooks for a full audit trail.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://black-sheep-finance.gitbook.io/black-sheep-finance-docs/guides/langchain.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
