---
title: "Tutorial: Control a coding agent's model access with Unity Gateway"
description: Route external coding agents like Claude Code, Codex, GitHub Copilot, Cursor, and Gemini CLI through governed model services in Unity Gateway, then control access and cap spend.
canonical_url: https://docs.databricks.com/gcp/en/ai-gateway/govern-coding-agent-models
---

# Tutorial: Control a coding agent's model access with Unity Gateway

In this tutorial, you connect an external coding agent, such as Claude Code, Codex, GitHub Copilot, Cursor, or Gemini CLI, to large language models (LLMs) governed by [Unity Gateway](https://docs.databricks.com/gcp/en/ai-gateway/index.md). You then control who can access those models and how much the agent can consume and spend. Instead of each developer pasting a provider API key into their tool, every request routes through a Unity Gateway [model service](https://docs.databricks.com/gcp/en/ai-gateway/model-services.md). Unity Catalog controls who can use which models, and Unity Gateway enforces limits and records usage and cost.

By the end of this tutorial, you have:

* A coding agent that sends every model request through Unity Gateway using your Databricks credentials, with no provider API key on the developer's machine.
* Central records of the agent's usage and cost, subject to the rate limits and permissions you set on the model service.

## Prerequisites

* A workspace enabled for Unity Catalog. See [Get started with Unity Catalog](https://docs.databricks.com/gcp/en/data-governance/unity-catalog/get-started.md).
* A model service to route traffic to, and `EXECUTE` on it. Databricks-hosted models are available as ready-to-use model services out of the box. See [Model services](https://docs.databricks.com/gcp/en/ai-gateway/model-services.md). To govern a model you manage yourself, create one first. See [Create a custom model service ](https://docs.databricks.com/gcp/en/ai-gateway/create-model-services.md#create-a-model-service).
* The [Databricks CLI](https://docs.databricks.com/gcp/en/dev-tools/cli/install.md), authenticated to your workspace.
* Your Databricks workspace URL, in the form `https://<workspace-url>`.

## Step 1: Connect your coding agent

You can connect the agent in one of two ways:

* **[Unity Gateway CLI](https://github.com/databricks/unity-gateway) (recommended)**: An open-source launcher that handles OAuth and writes each agent's configuration file, including the model service base URL and model. You don't manage API keys or endpoints by hand.
* **Manual configuration**: Set the model service base URL and model yourself in the tool's built-in settings (Claude Code, Codex, Cursor, or Gemini CLI). See [Supported coding and work agents](https://docs.databricks.com/gcp/en/ai-gateway/coding-agent-supported-agents.md).

This section uses the Unity Gateway CLI. Install it, then run the agent you want:

```bash
uv tool install git+https://github.com/databricks/unity-gateway
```

### Claude Code

```bash
ug claude
```

### Codex

```bash
ug codex
```

To run in non-interactive mode, pass the agent's own flag through the Unity Gateway CLI:

```bash
ug codex --full-auto
```

### Gemini CLI

```bash
ug gemini
```

### OpenCode

```bash
ug opencode
```

### GitHub Copilot

```bash
ug copilot
```

On first run, the Unity Gateway CLI prompts for your workspace URL and authenticates; later runs go straight to the agent.

## Step 2: Govern the agent's model access

You can govern a model service like any other Unity Catalog securable: grant `EXECUTE` to control who can use it, and use Unity Gateway to cap request volume and spend. Set up any of the following:

* **Manage who can access the model**: In Catalog Explorer, open the model service and grant `EXECUTE` to users or groups on the **Permissions** tab.
* **Cap request volume**: In Catalog Explorer, on the model service's **Overview** tab, click **Set up** under **Rate limits**. Set a requests-per-minute (QPM) or tokens-per-minute (TPM) [rate limit](https://docs.databricks.com/gcp/en/ai-gateway/rate-limits.md) for all requests on the endpoint, or per user.
* **Set a spend budget**: An account administrator can create a [budget](https://docs.databricks.com/gcp/en/ai-gateway/budgets.md) scoped to all Unity Gateway endpoints. The budget aggregates spend across your model services; set an alert threshold to be notified, or to block usage, before costs exceed it.

## Step 3: Verify that traffic is governed

Confirm that the agent's requests route through Unity Gateway and are recorded:

* **From the agent**: Send a prompt. The agent responds using the model you configured, with no provider API key on your machine.
* **From the usage system table**: Query [usage tracking](https://docs.databricks.com/gcp/en/ai-gateway/usage-tracking.md) to confirm that your model service records the requests:

  ```sql
  SELECT service_name, requester, status_code, COUNT(*) AS calls
  FROM system.ai_gateway.usage
  WHERE service_type = 'MODEL_SERVICE'
  GROUP BY service_name, requester, status_code
  ORDER BY calls DESC;
  ```

To analyze spend in more detail, see [Track and analyze AI spend with Unity Gateway](https://docs.databricks.com/gcp/en/ai-gateway/cost-observability.md).

## Next steps

* [Tracing](https://docs.databricks.com/gcp/en/ai-gateway/coding-agent-observability.md#tracing)
* [Tutorial: Control a coding agent's GitHub MCP access](https://docs.databricks.com/gcp/en/ai-gateway/govern-coding-agent-tutorial.md)
* [Tutorial: Add guardrails to a model service](https://docs.databricks.com/gcp/en/ai-gateway/moderate-tutorial.md)
* [Discover and govern access to model services](https://docs.databricks.com/gcp/en/ai-gateway/govern-model-services.md)
