# Tool-calling agent over third-party LLMs

This design serves teams committed to a model provider such as OpenAI or Anthropic, often with an agent framework such as LangGraph. A [Function](/en/documentation/platform/functions/) runs the agent loop and calls the provider's API at each step. It runs the tools that the model asks for against internal APIs and SaaS tools. [KV Store](/en/documentation/platform/kv-store/) keeps the state of a task between requests, and [SQL Database](/en/documentation/platform/sql-database/) keeps the history of every task.

The design implements the use case [Build AI agents](/en/documentation/use-cases/build-and-run-ai-workloads/build-ai-agents/), which builds it end to end.

## Architecture diagram

Solid arrows carry the request, and dotted arrows carry the response. Each label starts with the number of the dataflow step it belongs to.

```mermaid
%%{init: {"layout": "elk", "elk": {"cycleBreakingStrategy": "MODEL_ORDER"}, "themeVariables": {"fontSize": "13px"}, "flowchart": {"nodeSpacing": 24, "rankSpacing": 36, "padding": 8, "wrappingWidth": 150, "useMaxWidth": true}, "sequence": {"width": 120, "actorMargin": 30, "useMaxWidth": true}}}%%
flowchart TD
  Client(["Client"])
  App["Application"]
  Fn["Function: agent loop"]
  KV[("KV Store")]
  LLM[["third-party LLM provider"]]
  Tools[["internal APIs and SaaS tools"]]
  DB[("SQL Database")]
  Client -->|"1. task"| App
  App -->|"1. Run Function"| Fn
  Fn -->|"2. read, 5. write state"| KV
  Fn -->|"3. messages, tools"| LLM
  Fn -->|"4. tool call"| Tools
  Fn -->|"5. task event"| DB
  KV -.->|"2. state"| Fn
  LLM -.->|"3. tool call or answer"| Fn
  Tools -.->|"4. result"| Fn
  Fn -.->|"6. answer or status"| App
  App -.->|"6. answer or status"| Client
```

### Dataflow

1. A client sends a task to the agent API, on the domain of a [Workload](/en/documentation/platform/workloads/) whose deployment sets the [Application](/en/documentation/platform/applications/). A rule of the Application runs the agent's [Function Instance](/en/documentation/platform/applications/functions-instances/) with *Run Function*.
2. When the task started in an earlier request, the Function reads the state of the task from KV Store, by the key of the task.
3. The Function sends the messages of the task and the tool definitions to the provider's API with `fetch()`, with the provider key. The model answers with a tool call or with the final answer.
4. For each tool call, the Function calls the internal API or the SaaS tool with `fetch()`. It sends the result to the provider as the next message. Steps 3 and 4 repeat once per step of the task.
5. A task can continue in a later request, such as when it waits for a person to approve an action. The Function then writes the state of the task to KV Store. The Function also records each event of the task in SQL Database.
6. The Function returns the answer, or the status of the task, to the Application, which delivers it to the client. When a provider call fails, the Function moves the task to a second provider if the design has one, or ends the task.

The provider is on the path of every step: its latency adds once per step, and a failure can end the task at any step. The provider key, the deadline of each call, and the fallback to a second provider are decisions in the code of the Function. Only the provider calls and the calls to tools outside Azion leave Azion. The state and the history of each task stay in KV Store and SQL Database.

## Components

- **Functions**: runs the agent loop and the tools, in JavaScript on Azion Runtime. Each provider call and each tool call is a `fetch()` call. Functions caps the outbound calls and the wall-clock time of one invocation, so these caps bound the steps of one request. For the values, refer to [Functions limits](/en/documentation/platform/functions/limits/).
- **third-party LLM provider** (integration): reasons at each step and chooses the tools, such as a model at OpenAI or Anthropic. Azion does not operate it, so its availability, its rate limits, and its billing stay with the provider. The Function reads the provider key from an [environment variable](/en/documentation/platform/functions/environment-variables/), so the code carries no key.
- **LangGraph** (integration): the agent framework, a design option. The Function runs the LangGraph graph in place of a hand-written loop.
- **KV Store**: holds the state of a task between requests, under one key per task. A write is visible at once where it lands, and it converges everywhere else within 60 seconds, or within the `cacheTtl` that a read sets. Until then, a task resumed elsewhere can read the state that the write replaced. An expiration on the key drops a task that nobody resumes, and access to KV Store is requested through a support ticket.
- **SQL Database**: holds the history of the tasks, one row per event, so the completed tasks, the steps, and the approvals are counted with SQL. A Function writes rows through `Azion.Sql` or the Azion API. It reads through a read replica, where a recent write can take a minute or more to appear.
- **internal APIs and SaaS tools** (integration): the services that the tools act on. Each tool is a `fetch()` call from the Function, with credentials that the Function reads from environment variables.
- **Application**: serves the agent API on the domain of the Workload. Its rules run the Function on the paths of the agent with *Run Function*. That behavior requires Functions turned on in the Application, and a new Application has Functions on.

## Implementation

- [Build AI agents](/en/documentation/use-cases/build-and-run-ai-workloads/build-ai-agents/) - builds this design end to end, with an approval paused in KV Store and every task recorded in SQL Database.
- [Pause an agent for approval with KV Store](/en/documentation/guides/ai/agents-and-rag/pause-an-agent-for-approval-with-kv-store/) - stores a task in KV Store before an action a person approves, and resumes it on the decision.
- [Write rows to SQL Database from a function](/en/documentation/guides/application-development/data/write-sql-database-rows-from-a-function/) - sends the write statements of the history through the Azion API, and checks the result of every statement.
- [Run a function on one path, and roll it back](/en/documentation/guides/application-development/functions-and-runtime/run-a-function-on-one-path-and-roll-it-back/) - creates the rule that runs the agent Function on its path, and turns it off to roll back.
- [Deploy the LangGraph AI Agent Boilerplate](/en/documentation/guides/application-development/frameworks/langgraph-ai-agent-boilerplate/) - deploys a backend built with LangGraph that runs an agent with an OpenAI key, the framework option of this design.

## Related resources

- [Retrieval-augmented assistant over third-party LLMs](/en/documentation/use-cases/build-and-run-ai-workloads/ai-agent-third-party-llm.md): The other design with a third-party provider, which answers from retrieved documents instead of running tools.
- [How KV Store works](/en/documentation/platform/kv-store/how-it-works.md): The convergence window that a task resumed in a later request reads through.
- [fetch](/en/documentation/devtools/runtime/api-reference/fetch.md): How long one provider call or tool call can wait, and the errors that a failed call rejects with.
- [AI models](/en/documentation/platform/ai-inference/models.md): The models of AI Inference whose Tool calling capability reads Yes, for an agent that keeps the model on Azion.
