Metadata-Version: 2.4
Name: knowapi-langchain
Version: 0.3.0
Summary: Official records for AI agents, with the source on every answer. KnowAPI tools for LangChain and LangGraph.
License: MIT
Project-URL: Homepage, https://knowapi.dev/agents
Project-URL: Documentation, https://api.knowapi.dev/llms.txt
Keywords: x402,usdc,payments,langchain,langgraph,agents,mcp,public-data,official-records,knowapi
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Requires-Dist: langchain-core>=0.3
Provides-Extra: wallet
Requires-Dist: eth-account>=0.10; extra == "wallet"

# knowapi-langchain

Official records for AI agents, with the source on every answer: KnowAPI tools for LangChain and LangGraph.

```python
from knowapi_langchain import KnowAPIToolkit

tools = KnowAPIToolkit().get_tools()       # core tools; KnowAPIToolkit(profile="all") for every tool
```

With LangChain agents (built on LangGraph; needs `langchain` and a model package such as `langchain-openai`):

```python
from langchain.agents import create_agent
from langchain_openai import ChatOpenAI

agent = create_agent(ChatOpenAI(model="gpt-4o-mini"), tools)
reply = agent.invoke({"messages": [("user", "Is there a shortage of amoxicillin right now? Cite the source.")]})
print(reply["messages"][-1].content)
```

Without LangChain:

```python
from knowapi_langchain import KnowAPI
kn = KnowAPI()
kn.ask("Which medicines are currently in shortage?")
kn.request_data("Daily FDA drug recalls for pet food with brand and reason")  # build it; earn when others use it
```

**Free to start:** a starter credit shared across every API, then pay per call. A call that returns no records is not charged. The current terms are at <https://api.knowapi.dev/v2/free-allowance>. No membership is needed (x402 or prepaid credit pays per call); memberships (Starter, Pro, Business) add monthly allowances and the business applications: <https://api.knowapi.dev/v1/plans>. Ask: a new question is $0.25, asking the same question again is $0.0037 (the saved API returns the latest records). Creating a custom API is free.

## Usage telemetry

Requests send `User-Agent: knowapi-langchain/<version> (langchain)` (or `(direct)` for the plain client): SDK name, version and framework, nothing about you or your machine. KnowAPI uses it to count SDK usage.

The tool list is read live from `https://api.knowapi.dev/mcp`. Docs for agents: <https://api.knowapi.dev/llms.txt>.

## Paying as an agent (optional x402)

Agents without native x402 support can pay from a wallet **you** control, within limits **you** set. Nothing is paid unless you pass a wallet, and your key's free allowance and prepaid credit are always used first.

```python
import os
from eth_account import Account                      # pip install "knowapi-langchain[wallet]"
from knowapi_langchain import KnowAPI, KnowAPIToolkit, eth_account_wallet, SpendLimitError

kn = KnowAPI(
    api_key=os.environ.get("KNOWAPI_KEY"),             # optional: keeps your free allowance and credit
    wallet=eth_account_wallet(Account.from_key(os.environ["AGENT_WALLET_KEY"])),
    limits={"per_call_usd": 0.05, "daily_usd": 2, "total_usd": 20},
    auto_top_up={"usd": 5},                            # optional: refill the key once instead of paying per call
)
print(kn.quote(api_id, {"state": "CA"}))              # free: what this exact call costs
data = kn.query(api_id, {"state": "CA"})               # pays the 402 only if needed; data["paid"] says what was paid
tools = KnowAPIToolkit(client=kn).get_tools()          # query_api pays the same way when it reports payment_required
```

- **Limits** are checked before anything is signed (`SpendLimitError`); pass `store` to keep totals across restarts.
- **No double charges:** a retry after a lost response re-sends the same signed authorization (it can settle only once); a new payment is signed only after the server says the previous one was not taken; an unknown outcome stops with `PaymentOutcomeUnknownError`.
- **Insufficient funds** stop with `InsufficientFundsError`. Only USDC on Base is paid by default (`networks`); you can pin the receiver (`pay_to`). Empty answers are never charged.
- Any wallet works if it has `address` and `sign_typed_data(typed_data) -> hex signature`.
- **Memberships:** `kn.buy_membership("starter")` reads the live price and revision from `/v1/plans` and sends them with the purchase (a changed price is refused with 409, never charged). Without `api_key`, one x402 payment returns a new `api_key` holding the membership (shown once: store it); the client uses it from then on. A membership is one payment, so set `per_call_usd` at or above its price. Every 402, quote and `/v1/pricing` lists the same URLs as the `membership` purchase method.

