# Deploy on your own infrastructure: mistral agents serve Source: /studio/agents/learn/where-it-runs/serve > Register an agent and run its worker on your own infrastructure. `mistral agents serve` deploys your agent with Studio, on infrastructure you host. It starts a long-running process, the **worker**, on your machine or in your cluster, that runs the agent's sessions. Mistral stores each session, so the work survives a crash, and your app opens sessions by the agent's name. The agent's tools run on infrastructure you control. Use it to run the agent in production on your own infrastructure, or to try what a deployment does. ### How it works 1. Your app, or `mistral agents chat`, starts a session on the agent's name. Mistral stores the session and queues its turn. 2. Your worker polls that queue and takes the turn. 3. The worker runs the agent loop and the tools on your machine, calls the model, and records each finished step with Mistral. A worker that stops loses no finished step. You need a project with an [Agent](/studio/agents/learn/agent) that sets `name=`, and a `MISTRAL_API_KEY` whose user has a workspace role with Workflows access, such as Developer or Workspace Contributor; a workspace admin assigns it in the [admin console](https://admin.mistral.ai): **Organization** > **Workspaces** > your workspace > **Members**, as [`mistral agents deploy`](/studio/agents/learn/where-it-runs/deploy) explains. ### What the CLI loads `mistral agents info`, `mistral agents serve` and `mistral agents deploy` all need the same thing: the [Agent](/studio/agents/learn/agent) object you built in Python. By default they look for `agent` in `main.py`, so a project that is a single `main.py` runs as it is: ```python title="main.py" from mistralai import agents @agents.tool async def get_weather(city: str) -> str: """Get the current weather in a city.""" return f"It is 18°C and sunny in {city}." agent = agents.Agent( model="mistral-large-4", name="hello-agent", instructions="You are a friendly agent. Greet the user in one sentence.", tools=[get_weather], ) ``` The CLI imports `main.py`, so code at its top level runs on load. Put the code that runs the agent from a script, such as `asyncio.run(main())`, under `if __name__ == "__main__":`. To keep the agent somewhere else, declare it in `pyproject.toml` as `module:attr`: the module that defines it, then the variable that holds it: ```toml [tool.mistralai.agents] entrypoint = "src.service:support_agent" ``` The module is imported with the project root on the import path, so a module at the root works without the project being an installable package. Every command takes a project root as its argument, so `mistral agents serve ./hello-agent` runs one you are not standing in. #### The agent must be named `Agent(...)` has to set `name=` explicitly. It is the name `mistral agents serve` registers and the name `mistral agents deploy` routes on, so the CLI refuses to guess it: ```txt main:agent must set an explicit name: `agents.Agent(model=..., name=...)`. The name is the deployed workflow name. Set it in code even when you pass `--name`. ``` The name is unique **per workspace**, not globally: serving or deploying `hello-agent` again from the same workspace updates that same agent, but another workspace's `hello-agent` is a separate one. So two people who serve `hello-agent` in one workspace share one agent: each session goes to either process, and runs that person's code. While you build, give each person their own name, for example `mistral agents serve --name hello-agent-ada`. A name that contains a dot, such as `acme.bot`, is reserved for the platform's own agents: `Agent(...)` raises `agents.SdkError` for it. ### Serve the agent From the project's folder: ```bash export MISTRAL_API_KEY="" mistral agents serve # the project in the current directory mistral agents serve ./hello-agent --name hello-agent ``` `mistral agents serve` runs a worker, registers the agent under its name, and prints where to try it: ```txt ✓ Agent hello-agent is serving on agents-sdk-hello-agent Try the agent: • in Vibe Code Web: https://chat.mistral.ai/managed-agents/hello-agent • programmatically, using the Session API: https://docs.mistral.ai/studio/agents/learn/sessions Debug: https://console.mistral.ai/managed-agents//sessions?shareContext= Press Ctrl+C to stop. ``` The Debug link opens the agent's sessions in the console. `` is the agent's ID, and `` is your organization and workspace IDs, URL-encoded, so the console opens the right workspace. `--name` overrides the agent's `name=`, which is also what sessions are routed on. In your app, open sessions on the served agent by that name: ```python from mistralai import agents agent = agents.Agent.load_from(agent_name="hello-agent") ``` Stop `mistral agents serve` and the agent stops answering, but Mistral stores the sessions. A running turn usually resumes from its last finished step once `mistral agents serve` runs again. A tool call that was running is not made again by the SDK, but the model may call the tool again, so keep tools idempotent: see [Durability](/studio/agents/learn/where-it-runs#durability). > **Warning:** > > **Tools run on the machine that runs `mistral agents serve`** > > Every session on this agent, whether it comes from Vibe Code Web or from the Session API, runs its tools > on that machine. To isolate what the agent runs, give it a > [sandbox](/studio/agents/learn/environment). Try the agent by its name, from another terminal: ```bash mistral agents chat hello-agent ``` Output: ```txt Talking to hello-agent (session 01a1a89e-c09f-7043-85a7-af2170ff112d). Ctrl+D or /exit to quit. > What is the weather in Paris? → get_weather {"city": "Paris"} ← get_weather done It's currently **18°C and sunny** in Paris! ☀️ ``` ### Check what was loaded `mistral agents info` prints what the CLI loaded, and names the first thing it could not load. For the `main.py` above: ```txt hello-agent mistral-large-4 instructions You are a friendly agent. Greet the user in one sentence. sandbox none tools client.get_weather connectors (none) mcps (none) skills (none) guardrails (none) hooks (none) subagents (none) subagent policy (none) permissions auto · no policy ``` Run it after you change the agent's tools, connectors or hooks. A tool that does not appear here does not appear to the model either. See [`mistral agents info`](/studio/agents/reference/cli#agents-info) for its options. ### Set the environment Against production, `mistral agents serve` needs only `MISTRAL_API_KEY`. To point one at another deployment, set `MISTRAL_API_URL` and `AGENTS_SDK_DEPLOY_CONTROL_PLANE_URL` together: see [Environment](/studio/agents/reference/cli#environment). By default, a self-hosted session ends after 4 hours idle, or after 24 hours in total. Change these limits with `AGENTS_SDK_IDLE_TIMEOUT_SECONDS` and `AGENTS_SDK_LIFETIME_LIMIT_SECONDS`, in seconds, up to a year each: see [Session limits](/studio/agents/learn/where-it-runs/session-lifecycle#session-limits). `mistral agents serve` traces its turns to Mistral Studio by default: see [See your sessions and traces](/studio/agents/learn/where-it-runs/deploy#see-your-sessions-and-traces), and [Custom redaction](/studio/agents/learn/where-it-runs/deploy#custom-redaction) to choose what the traces mask. ### Code that replays Once deployed, the agent loop is replayed whenever a worker picks a turn back up, for example after a restart. To read the time, sleep, or make ids and random numbers, use `agents.now()`, `agents.sleep()`, `agents.uuid4()` and `agents.random()`: they give the same answer on replay. Tools and the agent's `hooks=` run outside the loop, as recorded steps, and the four functions work there too. A [sandbox hook](/studio/agents/learn/environment/sandbox-hooks) and a [redaction policy](/studio/agents/learn/where-it-runs/deploy#custom-redaction) run in the loop: use the four functions there. ### Common issues `mistral agents serve` prints only warnings and the banner. Set `AGENTS_SDK_DEBUG=1` for the full log, which names the Workflows API it resolved. `LOG_LEVEL` does not work here. - **`Cannot serve agent: MISTRAL_API_KEY was refused (HTTP 401).`** The next line reads `mistral agents serve needs a credential allowed to run agent workers in this workspace; check MISTRAL_API_KEY.` The key is wrong, expired or revoked: check it, or create a new one. - **`Cannot serve agent: registration failed at https://api.mistral.ai/v2/agents: `.** No session reaches the agent until its name points at your `mistral agents serve` process. `` names the cause: - **`the name hello-agent is already bound to another workflow or deployment`.** See [Freeing a name](/studio/agents/learn/where-it-runs/agent-lifecycle#freeing-a-name). - Other causes: an agents API that is not enabled for the account, or a control-plane URL that does not end in `/v2/agents`, which fails with 404 Not Found. - **`The user associated with this worker credential does not have permission to use Workflows in this workspace.`** Ask a workspace admin to assign the key's user a role with Workflows access, such as Developer or Workspace Contributor, in the [admin console](https://admin.mistral.ai): **Organization** > **Workspaces** > your workspace > **Members**. Then serve again. - **`MISTRAL_API_KEY is not set`.** Export it in your shell, or put it in the project's `.env`: `mistral agents serve` reads the key from either. ### Next steps