Tool-calling agent over third-party LLMs
Run an agent loop in a Function on Azion, with a third-party model choosing the tools at each step and KV Store holding the state between requests.
This design serves teams committed to a model provider such as OpenAI or Anthropic, often with an agent framework such as LangGraph. A Function 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 keeps the state of a task between requests, and SQL Database keeps the history of every task.
The design implements the use case 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.
Dataflow
- A client sends a task to the agent API, on the domain of a Workload whose deployment sets the Application. A rule of the Application runs the agent’s Function Instance with Run Function.
- 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.
- 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. - 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. - 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.
- 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. - 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, 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
cacheTtlthat 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.Sqlor 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 - 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 - 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 - 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 - creates the rule that runs the agent Function on its path, and turns it off to roll back.
- Deploy the LangGraph AI Agent Boilerplate - deploys a backend built with LangGraph that runs an agent with an OpenAI key, the framework option of this design.