Metadata-Version: 2.5
Name: langchain-accord
Version: 0.1.0
Summary: Accord for LangChain and LangGraph: agents that pay several sellers as one decision, only for work that passes your evaluator, within limits enforced when money moves.
Project-URL: Homepage, https://useaccord.ai
Project-URL: Documentation, https://useaccord.ai/quickstart/
Author: Accord
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: accord,ai agents,langchain,langgraph,payments
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: getaccord>=0.10.2
Requires-Dist: langchain-core<2,>=1.0
Provides-Extra: langgraph
Requires-Dist: langgraph<2,>=1.0; extra == 'langgraph'
Provides-Extra: test
Requires-Dist: langchain-tests>=1.0; extra == 'test'
Requires-Dist: langgraph-checkpoint-sqlite>=2; extra == 'test'
Requires-Dist: langgraph<2,>=1.0; extra == 'test'
Requires-Dist: pytest-asyncio>=0.23; extra == 'test'
Requires-Dist: pytest>=8; extra == 'test'
Description-Content-Type: text/markdown

# langchain-accord

Accord for LangChain and LangGraph: **agents that pay only for work that passes your evaluator.**

- **Several sellers, one decision.** Hold money with every seller a task might need, then pay the ones your terms call
  for in one step ("scout and (marketpulse or quantfeed)"), or call everything off and the money comes back.
- **Pay only for accepted work.** Delivered work waits for your evaluator; rejected work is never paid.
- **No double payment when a node reruns.** LangGraph reruns a node after an interrupt or a crash. A `purchase_ref`
  derived from the thread makes the rerun find the purchase the first run opened: nothing is held or paid twice.
- **Limits enforced when money moves.** The weekly budget, allowed sellers and sign-off threshold belong to the agent
  key's owner. The agent can't raise them, and it can't give itself a sign-off.
- **Seller text is marked as data**, and every purchase has a public proof page.

## Install

```bash
npx @getaccord/cli init        # a test key in ./.env, with test money
pip install langchain-accord   # add langgraph for the LangGraph helpers
```

The tools read the agent key from `ACCORD_AGENT_KEY` or `./.env`. No tool takes it as an argument or returns it.

## Tools

```python
from langchain.agents import create_agent
from langchain.agents.middleware import HumanInTheLoopMiddleware
from langgraph.checkpoint.memory import InMemorySaver
from langchain_accord import AccordToolkit, seller_content_note

toolkit = AccordToolkit()                      # x402=True adds quote_x402_url and fetch_x402_resource
agent = create_agent(
    "anthropic:claude-sonnet-5-5",
    toolkit.get_tools(),
    system_prompt=seller_content_note(),       # seller text is data, never instructions
    middleware=[HumanInTheLoopMiddleware(interrupt_on={n: True for n in toolkit.money_moving_tools()})],
    checkpointer=InMemorySaver(),
)
```

The tools have the same names, arguments and answers as Accord's MCP server:

| Tool | What it does |
|---|---|
| `get_spending_limits` | Money available, what's left of the weekly budget, the sellers this key may pay. |
| `find_sellers` | The seller directory: who sells what, at what price, and whether this key may pay each. |
| `start_purchase` | Opens a purchase (`budget_usd`, `mode` "all" or "each", `purchase_ref`, `reason`, ...). Nothing is paid. |
| `place_holds` | Holds money with one or more sellers, in one step. |
| `get_delivered_work` | Delivered work to check, marked as seller text. |
| `accept_work` / `reject_work` | Decide on delivered work: only accepted work is paid. |
| `complete_purchase` | Pays every seller in one step (mode "all": all or none). |
| `cancel_purchase` | Calls it off; held money comes back. |
| `start_linked_purchase` | Several purchases under one condition, e.g. `scout and (marketpulse or quantfeed)`. |
| `linked_purchase_status` | Each member's state and whether the condition can be met. |
| `settle_linked_purchase` | One step: pays the chosen members and calls off the rest, or calls everything off. |
| `quote_x402_url` / `fetch_x402_resource` | Web resources that charge per request (x402). Off unless `x402=True`. |

A refusal (over the weekly budget, a seller the key may not pay, a purchase waiting for sign-off) comes back as the
tool's answer, quoting the rule, so the model can tell the user. `purchase_id` is the handle `start_purchase` gives,
or the purchase's Accord ID (`0x...`), which works after a restart; a `link_id` works after a restart too.
`start_linked_purchase` also takes a `purchase_ref`, so a retry returns the same linked purchase.

## LangGraph

```python
from langchain_accord.langgraph import purchase_ref_for, run_with_sign_off, sign_off_or_interrupt, seller_content_note
```

- **`purchase_ref_for(config, step)`** gives `"<thread_id>:<step>"`: the same on every rerun of that node. Give each
  purchase in a thread its own `step`.
- **`sign_off_or_interrupt(err)`** pauses the graph when Accord says a purchase needs a person's sign-off (an
  `awaiting_approval` error, or a linked purchase's `status()` with a member waiting). The interrupt payload is
  `{"kind": "accord_sign_off", "message", "approve_at", "purchase_ids"}`: never a key or token. The person approves
  outside the graph (the dashboard at app.test.useaccord.ai); resume with `Command(resume=True)`.
- **`run_with_sign_off(step, purchase=intent)`** runs a step, pausing for sign-off and trying again after each resume.
- **`seller_content_note()`** is the line to put in your system prompt and your evaluator's prompt.

```python
def buy(state, config):
    intent = accord.intent(budget=230_00, purchase_ref=purchase_ref_for(config, "buy"))
    run_with_sign_off(lambda: intent.hold("scout", 200_00, type="deliver", ref="Research brief"), purchase=intent)
    return {"intent_id": intent.id}             # an ID in graph state, never the key
```

## Examples (test mode, each under a minute after `init`)

```bash
python examples/research_brief.py                      # flow B: hold with three sellers, evaluate, settle
python examples/research_brief.py --fail marketpulse   # marketpulse declines: quantfeed is paid instead
python examples/research_brief.py --sign-off           # the graph pauses until a person approves
python examples/crash_resume.py --demo                 # flow C: crash twice, resume, paid once
```

If a run goes past the key's sign-off threshold, the graph pauses for a person's approval. Add `--approve` to approve
as the key's owner (the owner token `init` saved in `~/.accord`, read by the example's runner, never by the graph).

`research_brief.py` uses a plain-code evaluator unless `ACCORD_EVAL_MODEL` names a chat model (for example
`anthropic:claude-sonnet-5-5`, with `pip install langchain langchain-anthropic`). Then the evaluator is its own model
call with no tools and structured output; accepting or rejecting is still decided in code.

## Keeping the key safe

- The key stays in `ACCORD_AGENT_KEY` or `./.env`. Keep it, and any owner token, out of graph state, `interrupt()`
  payloads, prompts and traces. These tools and helpers never put it there, and hide anything that looks like a key or
  token in their answers.
- On LangGraph Platform, the key goes in the deployment's secrets (LangChain's secret store). Give that key tight
  limits: a small weekly budget, only the sellers it needs, and a low sign-off threshold.
- The owner token never goes to the agent. Approvals happen in the dashboard, or for a test key with
  `accord.test.approve(...)` run by the person, outside the graph.

## Tests

```bash
pip install -e '.[test]'
pytest                                   # unit tests and LangChain's standard tool tests (no network)
cd <a folder where you ran init> && pytest <this folder>/tests/integration_tests   # test mode
```

Apache-2.0. Built by Mysten Labs.
