# LangChain

`langchain-arche` gives a LangChain agent Arche's SEC financial data as four
tools. It is built on the [Python SDK](https://docs.arche.fi/sdks/python), so the tools call the same
public routes and return the same fields.

## Install

```bash
pip install langchain-arche
```

## Create the tools

`arche_tools` returns a list of LangChain `StructuredTool` objects. Pass an API
key, or any other [`ArcheClient`](https://docs.arche.fi/sdks/python#create-a-client) argument:

```python
import os

from langchain_arche import arche_tools

tools = arche_tools(api_key=os.environ["ARCHE_API_KEY"])
```

Create a key in the [developer portal](https://app.arche.fi/keys). To share one
connection pool with code that already uses the SDK, pass the client instead:
`arche_tools(client=client)`.

## The tools

- `arche_resolve_company` (tool): Resolve a ticker to the CIK of the SEC registrant it named, now or on a
  past date given as `as_of`.
- `arche_statements` (tool): List a company's normalized income statements, balance sheets or cash
  flows, one `statement_type` at a time, newest first, with each figure's
  filing. Takes a CIK or ticker, and optionally `from_date`, `to_date` and
  `page_size`.
- `arche_as_of_financials` (tool): Return a company's statements exactly as they were known on `as_of_date`,
  with no later restatement, plus a hash and URL to verify the snapshot.
- `arche_verify_replay` (tool): Check that an earlier Arche answer, named by its `replay_token`, is
  reproduced from the same SEC data today.

## Use them in an agent

The tools work with any LangChain chat model that supports tool calling:

```python
import os

from langchain.agents import create_agent
from langchain_arche import arche_tools

agent = create_agent(
    "anthropic:claude-sonnet-5-5",
    tools=arche_tools(api_key=os.environ["ARCHE_API_KEY"]),
)

result = agent.invoke(
    {"messages": [{"role": "user", "content": "What was Apple's revenue as known on 2024-03-31?"}]}
)
print(result["messages"][-1].content)
```

## Verify an answer

A replay token names one Arche query at one point in time. It comes from the
[MCP](https://docs.arche.fi/mcp) as-of tool or from `POST /v1/query/execute`; see
[Deterministic replay](https://docs.arche.fi/coverage#deterministic-replay). Give the agent a token
and `arche_verify_replay` says whether the same SEC data still returns the same
answer. The [Verifiable agent answers](https://docs.arche.fi/guides/verifiable-agent-answers) guide
covers having an agent quote the token beside each figure.

## Next steps

- [Python SDK](https://docs.arche.fi/sdks/python): Install the typed Python client and run the quickstart flow in code.
- [Overview](https://docs.arche.fi/): Start here for the product model, first request path, and key entry points.
- [Quickstart](https://docs.arche.fi/quickstart): Authenticate, resolve a company, retrieve its statements, and run an as_of query.
