Skip to content
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ agentkit/
│ ├── langchain-cdp-smart-wallet-chatbot/
│ ├── langchain-cdp-solana-chatbot/
│ ├── langchain-eth-account-chatbot/
│ ├── langchain-krewe-inference-chatbot/
│ ├── langchain-nillion-secretvault-chatbot/
│ ├── langchain-twitter-chatbot/
│ ├── openai-agents-sdk-cdp-chatbot/
Expand Down
22 changes: 22 additions & 0 deletions python/examples/langchain-krewe-inference-chatbot/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Required
CDP_API_KEY_ID=
CDP_API_KEY_SECRET=
CDP_WALLET_SECRET=
OPENAI_API_KEY=

# Optional — krewe is mainnet-only today. Set to base-sepolia if you've
# pointed krewe at a sepolia orchestrator (see KREWE_PREDICT_URL below).
NETWORK_ID=base-mainnet

# Optional — override the krewe orchestrator URL (e.g. for self-hosted
# orchestrators or staging). Defaults to the production Railway orchestrator.
KREWE_PREDICT_URL=https://krewe-orchestrator-production.up.railway.app/v2/predict

# Optional — hard ceiling on USDC per call. Defaults to $0.10. Pricing today:
# $0.005 text.structure, $0.01 text.embed, $0.02 web.scrape, $0.05 text.complete.
KREWE_MAX_PAYMENT_USDC=0.10

# Optional account knobs (inherited from the CDP template)
ADDRESS=
IDEMPOTENCY_KEY=
RPC_URL=
29 changes: 29 additions & 0 deletions python/examples/langchain-krewe-inference-chatbot/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
ifneq (,$(wildcard ./.env))
include .env
endif

export

.PHONY: install
install:
uv sync

.PHONY: run
run:
uv run chatbot.py

.PHONY: format
format:
uv run ruff format .

.PHONY: format-check
format-check:
uv run ruff format . --check

.PHONY: lint
lint:
uv run ruff check .

.PHONY: lint-fix
lint-fix:
uv run ruff check . --fix
101 changes: 101 additions & 0 deletions python/examples/langchain-krewe-inference-chatbot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# krewe Inference + AgentKit + LangChain — Python Chatbot

This example shows how a Python LangChain agent can call **[krewe](https://www.krewe.world)** — a decentralized AI inference network on Base mainnet — using AgentKit's built-in `x402_action_provider`. Each inference call is paid per-request in USDC via the [x402 protocol](https://x402.org); no API keys or pre-paid quotas required.

The agent has a CDP-managed Base wallet and four inference tools through krewe:

| `kind` | Cost | What it does |
| ---------------- | ------ | --------------------------------------------------------------------- |
| `text.structure` | $0.005 | Regex/JSON extraction from unstructured text |
| `text.embed` | $0.01 | Sentence embeddings via a small quantized model |
| `web.scrape` | $0.02 | Fetch + clean a URL payload |
| `text.complete` | $0.05 | Small-language-model completion (greedy decode, server-pinned model) |

Every USDC paid through this example flows directly back to the miners that answered the job — krewe's orchestrator sweeps payments hourly, swaps them for `$KREW` on Uniswap V4, and deposits the proceeds into the registry's reward pool.

This is the Python sibling to the [TypeScript example](../../../typescript/examples/langchain-krewe-inference-chatbot/) — same orchestrator, same pricing, same auth flow, native Python idioms.

## Prompts to try

- `"Extract every email and date from this text: 'RSVP to hi@krewe.world by 2026-05-17 or call us 2026-05-20.'"`
- `"Embed this sentence: 'Decentralized AI inference on Base'."`
- `"Scrape https://www.krewe.world and tell me what the project does."`
- `"Use krewe's text.complete to write a one-sentence summary of x402."`

## Prerequisites

- **Python 3.10+** — verify with `python3 --version`. Install via [pyenv](https://github.com/pyenv/pyenv) if needed.
- **[uv](https://docs.astral.sh/uv/)** — the package manager this example uses.
- API keys:
- **CDP API key** — https://portal.cdp.coinbase.com/access/api
- **CDP wallet secret** — https://portal.cdp.coinbase.com/products/wallet-api
- **OpenAI API key** — https://platform.openai.com/docs/quickstart#create-and-export-an-api-key

Rename `.env.local` to `.env` and fill in:

```bash
CDP_API_KEY_ID=...
CDP_API_KEY_SECRET=...
CDP_WALLET_SECRET=...
OPENAI_API_KEY=...

NETWORK_ID=base-mainnet # krewe is mainnet-only today
KREWE_PREDICT_URL=https://krewe-orchestrator-production.up.railway.app/v2/predict
KREWE_MAX_PAYMENT_USDC=0.10 # safety cap per call
```

### Funding the agent

The agent's CDP wallet needs **USDC on Base mainnet** to pay for inference. The cheapest job (`text.structure`) costs $0.005, so $1 funds 200 calls. Send USDC to the wallet address printed at startup.

> Note: krewe's *production* orchestrator runs only on Base mainnet today. The `NETWORK_ID=base-sepolia` path works against a self-hosted orchestrator.

## Running the example

From the repository root:

```bash
cd python/examples/langchain-krewe-inference-chatbot
make install # uv sync — installs deps + the in-tree coinbase-agentkit
make run # uv run chatbot.py
```

You'll be prompted for **chat** (interactive prompts) or **auto** (autonomous loop that fires a creative krewe job every 30s).

## How the x402 handshake works under the hood

The flow is invisible to your agent code, but worth knowing:

1. Agent calls `https://krewe-orchestrator-production.up.railway.app/v2/predict` with a job body.
2. Orchestrator returns `402 Payment Required` with a signed EIP-3009 challenge specifying the USDC `value`, `to`, `nonce`, and validity window.
3. AgentKit's `x402_action_provider` signs the challenge with the CDP wallet (via the x402 facilitator).
4. Orchestrator verifies the signature, submits `transferWithAuthorization` on the USDC contract, waits for confirmation, then runs the inference job through krewe's miner network.
5. 2-of-3 byte-equal miner consensus produces the output, which is returned along with the on-chain `paymentTxHash`.

Full protocol writeup: [`docs/x402.md`](https://github.com/krewe-AI/krewe/blob/main/docs/x402.md) in the krewe repo.

## Without AgentKit — direct krewe Python SDK

If you only need to call krewe (not the full AgentKit stack), the standalone [`krewe-x402`](https://pypi.org/project/krewe-x402/) package is simpler — no CDP wallet provider needed, just an `eth_account.Account`:

```python
from eth_account import Account
from krewe_x402 import KreweClient

with KreweClient(account=Account.from_key("0x...")) as client:
result = client.predict(kind="text.structure",
payload={"text": "Email hi@krewe.world by 2026-05-17"})
print(result["output"])
```

## Want a self-hosted orchestrator?

The orchestrator is open-source and small (~2k LOC of TypeScript). See [`SETUP.md`](https://github.com/krewe-AI/krewe/blob/main/SETUP.md) in the krewe repo for the dev path, and [`docs/architecture.md`](https://github.com/krewe-AI/krewe/blob/main/docs/architecture.md) for the network model. Point `KREWE_PREDICT_URL` at your instance and this example works against it unchanged.

## Links

- krewe site: https://www.krewe.world
- krewe repo: https://github.com/krewe-AI/krewe
- `$KREW` on Basescan: https://basescan.org/address/0xc411E6deE1F70F57F08714D3bd54b4F36f904B07
- x402 spec: https://x402.org
- AgentKit docs: https://docs.cdp.coinbase.com/agentkit/docs/welcome
Loading
Loading