---
title: Run agents on Databricks Apps using the legacy agent server
description: Deploy an agent to Databricks Apps with the legacy MLflow AgentServer, and see how it compares to the Agent Bricks CLI, DurableAgentServer, and Agent Runtime.
canonical_url: https://docs.databricks.com/aws/en/agents/custom-agents/author-agent
---

# Run agents on Databricks Apps using the legacy agent server

You can deploy an agent to Databricks Apps with the MLflow `AgentServer`, an earlier agent server that the app templates use. For new agents, Databricks recommends the [Agent Bricks CLI](https://docs.databricks.com/aws/en/agents/custom-agents/agent-bricks-cli.md), which serves your agent with [`DurableAgentServer`](https://docs.databricks.com/aws/en/agents/deploy/agent-server.md) and deploys it to [Agent Runtime](https://docs.databricks.com/aws/en/agents/deploy/agent-runtime.md).

## <a id="compute-stack" />How this approach compares to Agent Server and Agent Runtime

A deployed agent has three layers: a framework or harness, an agent server, and an agent runtime. See [Deploy agents on Databricks](https://docs.databricks.com/aws/en/agents/deploy/index.md). The approach on this page runs on Databricks Apps, the same compute that Agent Runtime uses, but with a different agent server and a deployment that you configure yourself.

| Layer                | This page                                                                   | Agent Bricks CLI                                                                                                                                        |
| -------------------- | --------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Framework or harness | Any framework, wrapped in the MLflow `ResponsesAgent` interface             | Any framework, with templates for LangGraph and the OpenAI Agents SDK                                                                                   |
| Agent server         | MLflow `AgentServer`, or `LongRunningAgentServer` in the advanced templates | `DurableAgentServer`                                                                                                                                    |
| Agent runtime        | Databricks Apps. You configure the app, its resources, and its permissions. | Agent Runtime on Databricks Apps. `agentbricks deploy` provisions memory, session, and run state stores, grants access to them, and configures tracing. |
| Client API           | OpenAI Responses API at `/responses`                                        | Invocation API at `/api/invocations`                                                                                                                    |

The MLflow `AgentServer` doesn't provide the persistent run state, crash recovery, idempotent invocations, or ordered sessions that `DurableAgentServer` provides. `LongRunningAgentServer` adds background runs, stream resumption, and crash recovery, using a Lakebase database that you configure. To compare the agent servers, see [Compare agent servers on Databricks](https://docs.databricks.com/aws/en/agents/deploy/agent-server.md#agent-servers).

Use this page to build or maintain an agent from the [app templates](https://github.com/databricks/app-templates). For new agents, see:

* [Deploy agents on Databricks](https://docs.databricks.com/aws/en/agents/deploy/index.md)

* [Agent Bricks CLI](https://docs.databricks.com/aws/en/agents/custom-agents/agent-bricks-cli.md)

* [Agent Bricks quickstart](https://docs.databricks.com/aws/en/agents/quickstart.md)

## Build from the app templates

Build an agent and deploy it using Databricks Apps. Databricks Apps gives you full control over the agent code, server configuration, and deployment workflow. This approach is ideal when you need custom server behavior, git-based versioning, or local IDE development.

![Agent chat UI preview](https://assets.docs.databricks.com/_static/images/generative-ai/agent-chat-ui.gif)

Every conversational [agent template](https://github.com/databricks/app-templates) includes a built-in chat UI (shown above) with no additional setup required. The chat UI supports streaming responses, markdown rendering, Databricks authentication, and optional persistent chat history.

## Requirements

Enable Databricks Apps in your workspace. See [Set up your Databricks Apps workspace and development environment](https://docs.databricks.com/aws/en/dev-tools/databricks-apps/configure-env.md).

## <a id="examples" /> Step 1. Clone the agent app template

Get started by using a pre-built agent template from the [Databricks app templates repository](https://github.com/databricks/app-templates/tree/main/agent-openai-agents-sdk).

This tutorial uses the `agent-openai-agents-sdk` template, which includes:

* An agent created using OpenAI Agent SDK
* Starter code for an agent application with a conversational REST API and an interactive chat UI
* Code to evaluate the agent using MLflow

Choose one of the following paths to set up the template:

### Workspace UI

Install the app template using the Workspace UI. This installs the app and deploys it to a compute resource in your workspace. You can then sync the application files to your local environment for further development.

1. In your Databricks workspace, click **+ New** > **App**.

2. Click **Agents** > **Custom Agent (OpenAI SDK)**.

3. Create a new MLflow experiment with the name `openai-agents-template` and complete the rest of the set up to install the template.

4. After you create the app, click the app URL to open the chat UI.

After you create the app, download the source code to your local machine to customize it:

1. Copy the first command under **Sync the files**

   ![Sync files Databricks Apps](https://docs.databricks.com/aws/en/images/generative-ai/sync-files.png)

2. In a local terminal, run the copied command.

### Clone from GitHub

To start from a local environment, clone the agent template repository and open the `agent-openai-agents-sdk` directory:

```bash
git clone https://github.com/databricks/app-templates.git
cd app-templates/agent-openai-agents-sdk
```

## Step 2. Understand the agent application

The agent template demonstrates a production-ready architecture with these key components. Open the following sections for more details about each component:

![Agent on App simple diagram](https://docs.databricks.com/aws/en/images/generative-ai/agent-databricks-app-diagram.svg)

Open the following sections for more details about each component:

**![Chat icon](https://docs.databricks.com/aws/en/images/genie/chat-icon.png) Built-in chat UI**

The agent template automatically fetches and runs the [chat app template](https://docs.databricks.com/aws/en/agents/custom-agents/chat-app.md) as its frontend. This chat UI is bundled into the same Databricks Apps deployment and served alongside your agent, so there is no additional setup required.

You can customize the chat UI directly in your project. For more details on the chat app's features, including how to enable persistent chat history and user feedback collection, see [Build and share a chat UI with Databricks Apps](https://docs.databricks.com/aws/en/agents/custom-agents/chat-app.md).

**![Chip icon.](https://docs.databricks.com/aws/en/images/product-icons/ChipIcon.svg) MLflow AgentServer**

An async FastAPI server that handles agent requests with built-in tracing and observability. The [AgentServer](https://mlflow.org/docs/latest/genai/serving/agent-server/) provides the `/responses` endpoint for querying your agent and automatically manages request routing, logging, and error handling.

The agent template sets this up for you. It creates the server, connects it to your agent code, and configures your app to launch the server on deployment. Start from the template instead of wiring these pieces together by hand.

**![Brackets square icon.](https://docs.databricks.com/aws/en/images/product-icons/BracketsSquareIcon.svg) `ResponsesAgent` interface**

Databricks recommends MLflow [`ResponsesAgent`](https://mlflow.org/docs/latest/genai/serving/responses-agent) to build agents. `ResponsesAgent` lets you build agents with any third-party framework, then integrate it with Databricks AI features for robust logging, tracing, evaluation, deployment, and monitoring capabilities.

![ResponsesAgent easily wraps existing agents for Databricks compatibility.](https://docs.databricks.com/aws/en/images/generative-ai/responses-agent-overview.svg)

To learn how to create a `ResponsesAgent`, see the examples in [MLflow documentation - ResponsesAgent for Model Serving](https://mlflow.org/docs/latest/genai/serving/responses-agent).

`ResponsesAgent` provides the following benefits:

* Advanced agent capabilities
  * **Multi-agent support**
  * **Streaming output**: Stream the output in smaller chunks.
  * **Comprehensive tool-calling message history**: Return multiple messages, including intermediate tool-calling messages, for improved quality and conversation management.
  * **Tool-calling confirmation support**
  * **Long-running tool support**

* Streamlined development, deployment, and monitoring
  * **Author agents using any framework**: Wrap any existing agent using the `ResponsesAgent` interface to get out-of-the-box compatibility with AI Playground, Agent Evaluation, and Agent Monitoring.
  * **Typed authoring interfaces**: Write agent code using typed Python classes, benefiting from IDE and notebook autocomplete.
  * **Automatic tracing**: MLflow automatically aggregates streamed responses in traces for easier evaluation and display.
  * **Compatible with the OpenAI `Responses` schema**: See [OpenAI: Responses vs. ChatCompletion](https://platform.openai.com/docs/guides/responses-vs-chat-completions).

**![Robot icon.](https://docs.databricks.com/aws/en/images/product-icons/RobotIcon.svg) OpenAI Agents SDK**

The template uses the [OpenAI Agents SDK](https://openai.github.io/openai-agents-python/) as the agent framework for conversation management and tool orchestration. You can author agents using any framework. The key is wrapping your agent with MLflow `ResponsesAgent` interface.

**![Mcp icon.](https://docs.databricks.com/aws/en/images/product-icons/McpIcon.svg) MCP (Model Context Protocol) servers**

The template connects to Databricks MCP servers to give agents access to tools and data sources. See [MCPs](https://docs.databricks.com/aws/en/agents/mcp-tools/index.md).

## Author agents using AI coding assistants

Databricks recommends using AI coding assistants such as Claude, Cursor, and Copilot to author agents. Use the provided agent skills, in `/.claude/skills`, and the `AGENTS.md` file to help AI assistants understand the project structure, available tools, and best practices. Agents can automatically read those files to develop and deploy the Databricks Apps.

## <a id="add-tools" />Step 3. Add tools to your agent

Give your agent capabilities like querying databases, searching documents, or calling external APIs by connecting it to MCP servers. The agent template includes a default MCP server connection. To add more tools, configure additional MCP servers in your agent code and grant the required permissions in `databricks.yml`.

Follow [Use MCP tools in a Python agent](https://docs.databricks.com/aws/en/agents/custom-agents/use-mcp-tools.md) to discover tools and connect them with your framework. To choose a server, see [MCPs](https://docs.databricks.com/aws/en/agents/mcp-tools/index.md).

### <a id="local-python-tools" />Define local Python function tools

For operations that don't require external data sources or APIs, define tools directly in your agent code. These tools run in the same process as your agent and are useful for data transformations, calculations, or utility operations.

#### OpenAI Agents SDK

Use the `@function_tool` decorator from the OpenAI Agents SDK:

```python
from agents import Agent, function_tool

@function_tool
def get_current_time() -> str:
    """Get the current date and time."""
    from datetime import datetime
    return datetime.now().isoformat()

agent = Agent(
    name="My agent",
    instructions="You are a helpful assistant.",
    model="databricks-claude-sonnet-4-5",
    tools=[get_current_time],
)
```

#### LangGraph

Use the `@tool` decorator from LangChain:

```python
from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent
from databricks_langchain import ChatDatabricks

@tool
def get_current_time() -> str:
    """Get the current date and time."""
    from datetime import datetime
    return datetime.now().isoformat()

agent = create_react_agent(
    ChatDatabricks(endpoint="databricks-claude-sonnet-4-5"),
    tools=[get_current_time],
)
```

Local function tools don't require resource grants in `databricks.yml` because they run within the agent process.

## <a id="ai-gateway" /> Step 4. Govern LLM usage from your agents on Databricks Apps with Unity Gateway

Route your agent's LLM calls through [Unity Gateway](https://docs.databricks.com/aws/en/ai-gateway/index.md) so every request is governed by the same controls regardless of which provider answers it. With the gateway in the request path, you can centralize permissions, attribute cost per app, swap models, and inspect or replay traffic without modifying agent code or rotating provider credentials.

1. Point your agent at a Unity Gateway endpoint. In your agent code, pass the Unity Gateway endpoint name as the `model` argument and set `use_ai_gateway=True` on the Databricks LLM client. The client routes traffic through the gateway and handles authentication automatically.

   ### OpenAI

   ```python
   from agents import Agent, set_default_openai_api, set_default_openai_client
   from databricks_openai import AsyncDatabricksOpenAI

   set_default_openai_client(AsyncDatabricksOpenAI(use_ai_gateway=True))
   set_default_openai_api("chat_completions")

   agent = Agent(
       name="Agent",
       instructions="You are a helpful assistant.",
       model="<ai-gateway-endpoint>",
   )
   ```

   ### LangGraph

   ```python
   from databricks_langchain import ChatDatabricks

   llm = ChatDatabricks(
       model="<ai-gateway-endpoint>",
       use_ai_gateway=True,
   )
   ```

   For additional API surfaces (OpenAI Responses API, Anthropic Messages API, Google Gemini) and REST examples, see [Query foundation and embedding models](https://docs.databricks.com/aws/en/ai-gateway/query-model-services.md).

## Advanced authoring topics

**Streaming responses**

### <a id="streaming" /> Streaming responses

Streaming allows agents to send responses in real-time chunks instead of waiting for the complete response. To implement streaming with [`ResponsesAgent`](https://mlflow.org/docs/latest/api_reference/python_api/mlflow.pyfunc.html#mlflow.pyfunc.ResponsesAgent), emit a series of delta events followed by a final completion event:

1. **Emit delta events**: Send multiple [`output_text.delta`](https://platform.openai.com/docs/api-reference/responses_streaming/response/output_text/delta) events with the same `item_id` to stream text chunks in real-time.
2. **Finish with done event**: Send a final [`response.output_item.done`](https://platform.openai.com/docs/api-reference/responses_streaming/response/output_item/done) event with the same `item_id` as the delta events containing the complete final output text.

Each delta event streams a chunk of text to the client. The final done event contains the complete response text and signals Databricks to do the following:

* Trace your agent's output with MLflow tracing
* Aggregate streamed responses in Unity Gateway inference tables
* Show the complete output in the AI Playground UI

**Streaming error propagation**

Databricks propagates any errors encountered while streaming with the last token under `databricks_output.error`. It is up to the calling client to properly handle and surface this error.

```bash
{
  "delta": …,
  "databricks_output": {
    "trace": {...},
    "error": {
      "error_code": BAD_REQUEST,
      "message": "TimeoutException: Tool XYZ failed to execute."
    }
  }
}
```

**Custom inputs and outputs**

### <a id="custom-schema" /> Custom inputs and outputs

Some scenarios might require additional agent inputs, such as `client_type` and `session_id`, or outputs like retrieval source links that should not be included in the chat history for future interactions.

For these scenarios, MLflow [`ResponsesAgent`](https://mlflow.org/docs/latest/api_reference/python_api/mlflow.pyfunc.html#mlflow.pyfunc.ResponsesAgent) natively supports the fields `custom_inputs` and `custom_outputs`. You can access the custom inputs via `request.custom_inputs` in the framework examples above.

The [Agent Evaluation review app](https://docs.databricks.com/aws/en/mlflow3/genai/human-feedback/index.md) does not support rendering traces for agents with additional input fields.

**Provide `custom_inputs` in the AI Playground and review app**

If your agent accepts additional inputs using the `custom_inputs` field, you can manually provide these inputs in both [AI Playground](https://docs.databricks.com/aws/en/large-language-models/ai-playground.md) and the [review app](https://docs.databricks.com/aws/en/mlflow3/genai/human-feedback/index.md).

1. In either the AI Playground or the Agent Review App, select the gear icon ![Gear icon.](https://docs.databricks.com/aws/en/images/product-icons/GearIcon.svg).
2. Enable **custom\_inputs**.
3. Provide a JSON object that matches your agent's defined input schema.

   ![Provide custom\_inputs in the AI playground.](https://docs.databricks.com/aws/en/images/generative-ai/playground-custom-input.png)

## Step 5. Run the agent app locally

Set up your local environment:

1. Install `uv` (Python package manager), `nvm` (Node version manager), and the Databricks CLI:
   * [`uv` installation](https://docs.astral.sh/uv/getting-started/installation/)
   * [`nvm` installation](https://github.com/nvm-sh/nvm?tab=readme-ov-file#installing-and-updating)
   * Run the following to use Node 20 LTS:
     ```bash
     nvm use 20
     ```
   * [`databricks CLI` installation](https://docs.databricks.com/aws/en/dev-tools/cli/install.md)

2. Change directory to the `agent-openai-agents-sdk` folder.

3. Run the provided quickstart scripts to install dependencies, set up your environment, and start the app.

   ```bash
   uv run quickstart
   uv run start-app
   ```

In a browser, go to `http://localhost:8000` to open the built-in chat UI and start chatting with the agent.

## <a id="authentication" /> Step 6. Configure authentication

Your agent needs authentication to access Databricks resources. Databricks Apps provides two authentication methods: app authorization (service principal) and user authorization (on-behalf-of-user). You can configure either one through the workspace UI or declaratively in `databricks.yml` with Declarative Automation Bundles. The agent templates ship with a `databricks.yml`, so that path is the default when you start from a template.

For the complete reference, including all supported resource types, permission values, and an end-to-end `databricks.yml` walkthrough, see [Authentication for agents](https://docs.databricks.com/aws/en/agents/custom-agents/agent-authentication.md).

### App authorization (default)

App authorization uses a service principal that Databricks automatically creates for your app. All users share the same permissions.

Declare every resource the agent uses under `resources.apps.<app>.resources` in `databricks.yml`. Deploy the bundle to grant the service principal the declared permissions:

```yaml
resources:
  apps:
    agent_openai_agents_sdk:
      name: 'agent-openai-agents-sdk'
      source_code_path: ./
      config:
        command: ['uv', 'run', 'start-app']
        env:
          - name: MLFLOW_TRACKING_URI
            value: 'databricks'
          - name: MLFLOW_REGISTRY_URI
            value: 'databricks-uc'
          - name: MLFLOW_EXPERIMENT_ID
            value_from: 'experiment'
      resources:
        - name: 'experiment'
          experiment:
            experiment_id: '<experiment-id>'
            permission: 'CAN_EDIT'
        - name: 'llm'
          serving_endpoint:
            name: 'databricks-claude-sonnet-4-5'
            permission: 'CAN_QUERY'
```

```bash
databricks bundle deploy
databricks bundle run agent_openai_agents_sdk
```

For the full list of resource types, see [App authorization](https://docs.databricks.com/aws/en/agents/custom-agents/agent-authentication.md#app-authorization).

### User authorization

User authorization lets your agent act with each user's individual permissions. Use this when you need per-user access control or audit trails.

Add this code to your agent:

```python
from agent_server.utils import get_user_workspace_client

# In your agent code (inside @invoke or @stream)
user_workspace = get_user_workspace_client()

# Access resources with the user's permissions
response = user_workspace.serving_endpoints.query(name="my-endpoint", inputs=inputs)
```

> [!IMPORTANT]
>
> Initialize `get_user_workspace_client()` inside your `@invoke` or `@stream` functions, not during app startup. User credentials only exist when handling a request.

Configure which Databricks APIs the agent can call on the user's behalf by adding scopes under `user_api_scopes` on the app in `databricks.yml`:

```yaml
resources:
  apps:
    agent_openai_agents_sdk:
      name: 'agent-openai-agents-sdk'
      source_code_path: ./
      user_api_scopes:
        - sql
        - genie
        - model-serving
```

```bash
databricks bundle deploy
databricks bundle run agent_openai_agents_sdk
```

For the list of available scopes and complete setup instructions, see [User authorization](https://docs.databricks.com/aws/en/agents/custom-agents/agent-authentication.md#user-authorization).

## Step 7. Evaluate the agent

The template includes agent evaluation code. See `agent_server/evaluate_agent.py` for more information. Evaluate the relevance and safety of your agent's responses by running the following in a terminal:

```bash
uv run agent-evaluate
```

## Step 8. Deploy the agent to Databricks Apps

After configuring authentication, deploy your agent to Databricks. The agent templates use [Databricks Asset Bundles (DABs)](https://docs.databricks.com/aws/en/dev-tools/bundles/index.md) for deployment. The `databricks.yml` file in the template defines the app configuration and resource permissions. Ensure you have the [Databricks CLI](https://docs.databricks.com/aws/en/dev-tools/cli/tutorial.md) installed and configured.

> [!NOTE]
>
> For your app to appear in the **Agents** list in the workspace, its name must start with the `agent-` prefix (for example, `agent-openai-agents-sdk`). Set this in the `name` field under `resources.apps` in `databricks.yml`. The [agent templates](https://github.com/databricks/app-templates) already follow this convention, so apps created from a template appear automatically.

> [!NOTE]
>
> If you created your app through the Workspace UI in Step 1, run `databricks bundle deployment bind agent_openai_agents_sdk <app-name> --auto-approve` before deploying to bind the existing app to your bundle. Otherwise, `databricks bundle deploy` fails with "An app with the same name already exists".

1. Validate the bundle configuration to catch errors before deploying:

   ```bash
   databricks bundle validate
   ```

2. Deploy the bundle. This uploads your code and configures resources (MLflow experiment, serving endpoints, and so on) defined in `databricks.yml`:

   ```bash
   databricks bundle deploy
   ```

3. Start or restart the app:

   ```bash
   databricks bundle run agent_openai_agents_sdk
   ```

   > [!NOTE]
   >
   > `bundle deploy` only uploads files and configures resources. `bundle run` is required to start or restart the app with the new code.

For future updates, run `databricks bundle deploy` and then `databricks bundle run agent_openai_agents_sdk` to redeploy.

## Step 9. Query the deployed agent

The following example uses a quick `curl` request with an OAuth token. Personal access tokens (PATs) are not supported for Databricks Apps.

For the full list of query methods, including the Databricks OpenAI Client and REST API, see [Query an agent that uses the legacy MLflow `AgentServer`](https://docs.databricks.com/aws/en/agents/query-llms.md#agentserver).

Generate an OAuth token using the Databricks CLI:

```bash
databricks auth login --host <https://host.databricks.com>
databricks auth token
```

Use the token to query the agent:

```bash
curl -X POST <app-url.databricksapps.com>/responses \
   -H "Authorization: Bearer <oauth token>" \
   -H "Content-Type: application/json" \
   -d '{ "input": [{ "role": "user", "content": "hi" }], "stream": true }'
```

## Understand model signatures to ensure compatibility with Databricks features

Databricks uses [MLflow Model Signatures](https://mlflow.org/docs/latest/model/signatures.html) to define agents' input and output schema. Product features like the AI Playground assume that your agent has one of a set of supported model signatures.

If you follow the recommended approach to authoring agents using the ResponsesAgent interface, MLflow will automatically infer a signature for your agent that is compatible with Databricks product features.

## Limitations

* Only medium and large compute sizes are supported. See [Configure compute resources for a Databricks app](https://docs.databricks.com/aws/en/dev-tools/databricks-apps/compute-size.md).
* The [MLflow Review App Chat UI](https://docs.databricks.com/aws/en/mlflow3/genai/human-feedback/expert-feedback/live-app-testing.md) does not currently support agents deployed on Databricks Apps. To evaluate existing traces, use [labeling sessions](https://docs.databricks.com/aws/en/mlflow3/genai/human-feedback/expert-feedback/label-existing-traces.md), which work regardless of deployment method. Databricks is building review and feedback support directly into the [chatbot template](https://docs.databricks.com/aws/en/agents/custom-agents/chat-app.md).

## Next steps

Once your agent works in development, take it to production. See [Productionize your Databricks Apps agent](https://docs.databricks.com/aws/en/agents/custom-agents/productionize-agent.md) for the recommended sequence: CI/CD, load testing, then Unity Gateway.
