Where an AI agent runs and what shapes that choice

An AI agent is a program that sends instructions to a language model, reads the reply, and then takes steps such as calling a web service, querying a database or writing a file. Hosting one in the cloud means choosing three things: where the model runs, what kind of compute runs the agent's loop, and what permissions that loop receives. Each choice affects cost, response speed and how much damage a mistake can cause.

Most teams do not run the model themselves. They call a model through a managed service such as Amazon Bedrock, Azure AI Foundry or Google Cloud's Vertex AI, or through a model provider's own API. The agent code runs on compute you manage, such as AWS Lambda, Google Cloud Run, Azure Container Apps or a Kubernetes cluster. Keeping these two layers separate makes the rest of the decisions easier to follow.

The useful question is not which cloud is best. It is how long one task runs, how many steps it can take, and what it can reach if something goes wrong. Those answers point to the runtime, the permissions and the monitoring you will need. Prices, model names and service limits change often, so check each provider's current documentation before you settle on a design.

Key Takeaways

The points below summarise the decisions covered in this educational guide.

  • Separate the model layer, which is usually a managed API, from the agent layer, which is the code and compute you deploy yourself.
  • Match the runtime to task length, since serverless functions often stop after a set time limit while containers and worker services can run longer.
  • Give each agent its own cloud identity with only the permissions its tools need, and keep API keys in a dedicated secrets manager rather than in code.
  • Cap the number of steps and the spend allowed per task, because a looping agent can make many paid model calls before anyone reviews the bill.
  • Log every model call and tool call with enough detail to replay a failed run, and set budget alerts in your cloud account.

Managed model APIs compared with self-hosted GPUs

The biggest early decision is whether to call a model someone else runs or to host an open-weight model on your own GPU instances. A managed service removes most of the infrastructure work. You pick a model from a catalogue, send requests and pay for what you use. The trade-off is that the provider controls when models are retired, how they are updated and where processing happens, so you should read the data handling terms closely.

Self-hosting gives you control over the model version and the hardware. It also means you pay for GPU time whether or not the agent is busy, unless you configure the system to scale down to zero. Patching, capacity planning and handling traffic spikes become your job. For most first deployments, a managed API is the lower-effort route, and self-hosting makes sense once you have a clear reason, such as strict data residency rules or a model the managed catalogue does not offer.

ApproachWho runs the modelHow cost usually worksMain trade-off
Managed model service (Amazon Bedrock, Azure AI Foundry, Vertex AI)The cloud providerUsually billed per token or per request; rates vary by model and regionLess control over model versions and retirement dates
Model vendor's direct APIThe model vendorPer token or per request; rates vary by modelA separate contract and data terms to review
Open-weight model on rented GPUsYouHourly instance charges, which continue while the instance runsMore setup, patching and capacity planning

Picking a runtime for long-running agent tasks

An agent that answers one question in a few seconds fits well on a serverless function. An agent that searches documents, calls several tools and waits on external systems can run for minutes. Serverless functions usually have a maximum execution time, and the exact limit depends on the platform and its settings, so check it before you design around it. If the agent may exceed that limit, a container service or a queue-driven worker is usually a better fit.

Containers give you more control over memory, background processes and dependencies. Container platforms such as Cloud Run, Azure Container Apps and Kubernetes can scale instances up and down with traffic, though cold starts and minimum instance settings affect both speed and cost. A queue such as Amazon SQS or Google Cloud Pub/Sub lets you accept a task, return a job ID, and let a worker process it in the background. That pattern suits agents well because it keeps user-facing requests short and lets failed jobs retry.

Common mistakes at this stage include leaving the default timeout in place, running agents on a single instance with no retry logic, and storing conversation state only in memory. When the instance restarts, the agent loses track of where it was. Store intermediate state in a database or object store so a retried job can pick up from its last completed step.

Giving agent tools permissions without over-granting them

An agent is only as risky as the tools you connect to it. If it can send email, edit files or run database queries, every one of those actions runs under some identity. In most clouds that identity is a role or service account. The principle to follow is least privilege: grant only the specific actions and resources the agent needs for its task, and nothing broader. A support-summary agent that reads tickets should not hold permission to delete them.

Keep credentials out of prompts and source code. Store API keys and database passwords in a managed secrets service, such as AWS Secrets Manager, Google Secret Manager or Azure Key Vault, and have the runtime fetch them at start-up. Be cautious with any tool that runs generated code or shell commands. Run those in an isolated sandbox, and require a human to approve actions that cannot be undone, such as payments, deletions or outbound messages to customers.

Also review what text the agent can read. Web pages, uploaded documents and emails can contain instructions aimed at the model rather than the user. Treat tool outputs as data, not commands, and limit what a single agent run can change before a person reviews it.

Watching costs, logs and loops after deployment

Agent costs can rise quickly because each step may trigger another model call. A loop that retries a failing tool or re-reads a long document can multiply token usage in minutes. Set a maximum number of steps per run and a maximum spend per task inside the agent code. Then add budget alerts in your cloud billing console so you hear about unusual spending early, even if the code limits fail.

Logging is the other half. For each run, record the inputs, the model name and version, each tool call with its arguments and result, the final output and the error if one occurred. Tracing tools that follow the OpenTelemetry standard, or the observability features built into your cloud provider, can connect these steps into one timeline. Be careful about what you log: prompts and tool results may contain personal data, so apply retention limits and restrict who can read the logs.

Finally, test failure paths before real users arrive. Simulate a tool timing out, a model returning malformed output, and a loop that never reaches an answer. Confirm that the run stops, that the error is recorded, and that nothing irreversible happened.

Frequently Asked Questions

Do I need a GPU to run an AI agent in the cloud?

Not usually. Most agents call a hosted model through an API, so the agent code itself runs on ordinary CPU compute such as a function or a small container. A GPU becomes relevant only when you host an open-weight model yourself. In that case, GPU instances are billed by the hour, and their cost depends on the instance type and region, which vary by provider.

Which cloud service should I pick for an agent?

The right choice depends on task length, the cloud you already use and your team's experience. Serverless functions suit short tasks, while container services suit longer ones. Check each provider's current limits, pricing pages and regional availability before deciding, since these change over time and differ between services.

How do I stop an agent from running up a big bill?

Set a maximum step count and a spend ceiling for each task inside the agent code, so one run cannot loop without limit. Then create budget alerts in your cloud billing console. Review token usage by agent and by tool, because the expensive part is often one repeated step rather than the whole workflow.

Can an agent be trusted to act on its own?

Some actions can be automated safely, such as reading documents or drafting text for review. Actions that cannot be reversed, such as sending payments, deleting records or contacting customers, usually work better with a person approving them first. Limit the agent's permissions to match the actions you have reviewed and tested.