---
title: Log and register agents (Model Serving)
description: Learn how to create, parameterize, and log AI application agents.
canonical_url: https://docs.databricks.com/aws/en/agents/custom-agents/model-serving/log-agent
---

# Log and register agents (Model Serving)

> [!NOTE]
>
> For new agents, Databricks recommends the [Agent Bricks CLI](https://docs.databricks.com/aws/en/agents/custom-agents/agent-bricks-cli.md). The CLI deploys your agent with the [Agent Bricks agent server](https://docs.databricks.com/aws/en/agents/deploy/agent-server.md) on [Agent Runtime](https://docs.databricks.com/aws/en/agents/deploy/agent-runtime.md). See [Deploy agents on Databricks](https://docs.databricks.com/aws/en/agents/deploy/index.md). To move an existing agent from Model Serving to Databricks Apps, see [Migrate an agent from Model Serving to Databricks Apps](https://docs.databricks.com/aws/en/agents/custom-agents/migrate-agent-to-apps.md).

Log agents using Custom Agents. Logging an agent forms the basis of the development process. Logging captures a "point in time" of the agent's code and configuration so you can evaluate its quality.

## Requirements

[Create an agent](https://docs.databricks.com/aws/en/agents/index.md) before logging it.

Databricks recommends installing the latest version of the `databricks-sdk`.

```python
% pip install databricks-sdk
```

## <a id="code-based-logging" /> Code-based logging

Databricks recommends using MLflow's [Models from Code functionality](https://mlflow.org/docs/latest/model/models-from-code) when logging agents.

In this approach, the agent's code is captured as a Python file, and the Python environment is captured as a list of packages. When the agent is deployed, the Python environment is restored, and the agent's code is run to load the agent into memory so it can be invoked when the endpoint is called.

You can couple this approach with the use of pre-deployment validation APIs like [mlflow.models.predict()](https://www.mlflow.org/docs/latest/models#validate-models-before-deployment) to ensure that the agent runs reliably when deployed for serving.

To see an example of code-based logging, see [ResponsesAgent authoring example notebooks](https://docs.databricks.com/aws/en/agents/custom-agents/model-serving/author-agent-model-serving.md).

## <a id="infer-signature" />Infer Model Signature during logging

> [!NOTE]
>
> Databricks recommends authoring an agent using the [ResponsesAgent interface](https://docs.databricks.com/aws/en/agents/custom-agents/model-serving/author-agent-model-serving.md).
> If using ResponsesAgent, you can skip this section; MLflow automatically infers a valid signature for your agent.

If not using the `ResponsesAgent` interface, you must use one of the following methods to specify your agent's MLflow [Model Signature](https://mlflow.org/docs/latest/model/signatures) at logging time:

1. Manually define the signature
2. Use MLflow's Model Signature inferencing capabilities to automatically generate the agent's signature based on an input example you provide. This approach is more convenient than manually defining the signature.

The MLflow model signature validates inputs and outputs to ensure the agent interacts correctly with
downstream tools like AI Playground and the review app. It also guides other applications on how to use the agent
effectively.

The [LangChain](https://docs.databricks.com/aws/en/agents/custom-agents/model-serving/log-agent.md#code-log-steps) and [PyFunc](https://docs.databricks.com/aws/en/agents/custom-agents/model-serving/log-agent.md#pyfunc-logging) examples below use Model Signature inferencing.

If you would rather explicitly define a Model Signature yourself at logging time, see [MLflow docs - How to log models with signatures](https://mlflow.org/docs/latest/model/signatures#how-to-log-models-with-signatures).

## <a id="code-log-steps" />Code-based logging with LangChain

The following instructions and code sample show you how to log an agent with LangChain.

1. Create a notebook or Python file with your code. For this example, the notebook or file is named `agent.py`. The notebook or file must contain a LangChain agent, referred to here as `lc_agent`.

2. Include [mlflow.models.set\_model(lc\_agent)](https://mlflow.org/docs/latest/python_api/mlflow.models.html#mlflow.models.set_model) in the notebook or file.

3. Create a new notebook to serve as the driver notebook (called `driver.py` in this example).

4. In the driver notebook, use the following code to run `agent.py` and log the results to an MLflow model:

   ```python
   mlflow.langchain.log_model(lc_model="/path/to/agent.py", resources=list_of_databricks_resources)
   ```

   The `resources` parameter declares Databricks-managed resources needed to serve the agent, such as an AI Search index or serving endpoint that serves a foundation model. For more information, see [Implement automatic authentication passthrough](https://docs.databricks.com/aws/en/agents/custom-agents/model-serving/agent-authentication-model-serving.md#log-resources-automatic).

5. Deploy the model. See [Deploy an agent for AI applications (Model Serving)](https://docs.databricks.com/aws/en/agents/custom-agents/model-serving/deploy-agent.md).

6. When the serving environment is loaded, `agent.py` is run.

7. When a serving request comes in, `lc_agent.invoke(...)` is called.

```python

import mlflow

code_path = "/Workspace/Users/first.last/agent.py"
config_path = "/Workspace/Users/first.last/config.yml"

# Input example used by MLflow to infer Model Signature
input_example = {
  "messages": [
    {
      "role": "user",
      "content": "What is Retrieval-augmented Generation?",
    }
  ]
}

# example using langchain
with mlflow.start_run():
  logged_agent_info = mlflow.langchain.log_model(
    lc_model=code_path,
    model_config=config_path, # If you specify this parameter, this configuration is used by agent code. The development_config is overwritten.
    artifact_path="agent", # This string is used as the path inside the MLflow model where artifacts are stored
    input_example=input_example, # Must be a valid input to the agent
    example_no_conversion=True, # Required
  )

print(f"MLflow Run: {logged_agent_info.run_id}")
print(f"Model URI: {logged_agent_info.model_uri}")

# To verify that the model has been logged correctly, load the agent and call `invoke`:
model = mlflow.langchain.load_model(logged_agent_info.model_uri)
model.invoke(example)
```

## <a id="pyfunc-logging" />Code-based logging with PyFunc

The following instructions and code sample show you how to log an agent with PyFunc.

1. Create a notebook or Python file with your code. For this example, the notebook or file is named `agent.py`. The notebook or file must contain a PyFunc class, named `PyFuncClass`.

2. Include [`mlflow.models.set_model(PyFuncClass)`](https://mlflow.org/docs/latest/python_api/mlflow.models.html#mlflow.models.set_model) in the notebook or file.

3. Create a new notebook to serve as the driver notebook (called `driver.py` in this example).

4. In the driver notebook, use the following code to run `agent.py` and use [`log_model()`](https://mlflow.org/docs/latest/api_reference/python_api/mlflow.pyfunc.html#mlflow.pyfunc.log_model) to log the results to an MLflow model:

   ```python
   mlflow.pyfunc.log_model(python_model="/path/to/agent.py", resources=list_of_databricks_resources)
   ```

   The `resources` parameter declares Databricks-managed resources needed to serve the agent, such as an AI Search index or serving endpoint that serves a foundation model. For more information, see [Implement automatic authentication passthrough](https://docs.databricks.com/aws/en/agents/custom-agents/model-serving/agent-authentication-model-serving.md#log-resources-automatic).

5. Deploy the model. See [Deploy an agent for AI applications (Model Serving)](https://docs.databricks.com/aws/en/agents/custom-agents/model-serving/deploy-agent.md).

6. When the serving environment is loaded, `agent.py` is run.

7. When a serving request comes in, `PyFuncClass.predict(...)` is called.

```python
import mlflow
from mlflow.models.resources import (
    DatabricksServingEndpoint,
    DatabricksVectorSearchIndex,
)

code_path = "/Workspace/Users/first.last/agent.py"
config_path = "/Workspace/Users/first.last/config.yml"

# Input example used by MLflow to infer Model Signature
input_example = {
  "messages": [
    {
      "role": "user",
      "content": "What is Retrieval-augmented Generation?",
    }
  ]
}

with mlflow.start_run():
  logged_agent_info = mlflow.pyfunc.log_model(
    python_model=agent_notebook_path,
    artifact_path="agent",
    input_example=input_example,
    resources=resources_path,
    example_no_conversion=True,
    resources=[
      DatabricksServingEndpoint(endpoint_name="databricks-meta-llama-3-3-70b-instruct"),
      DatabricksVectorSearchIndex(index_name="prod.agents.databricks_docs_index"),
    ]
  )

print(f"MLflow Run: {logged_agent_info.run_id}")
print(f"Model URI: {logged_agent_info.model_uri}")

# To verify that the model has been logged correctly, load the agent and call `invoke`:
model = mlflow.pyfunc.load_model(logged_agent_info.model_uri)
model.invoke(example)
```

## <a id="authentication-for-databricks-resources" />Authentication for Databricks resources

Agents often need to authenticate to other resources to complete tasks. For example, a deployed agent might need to access an AI Search index to query unstructured data, or access the Prompt Registry to load dynamic prompts.

[Automatic authentication passthrough](https://docs.databricks.com/aws/en/agents/custom-agents/model-serving/agent-authentication-model-serving.md#log-resources-automatic) and [on-behalf-of authentication](https://docs.databricks.com/aws/en/agents/custom-agents/model-serving/agent-authentication-model-serving.md#log-resources-obo) require configuration during agent logging.

## <a id="register-agent" />Register the agent to Unity Catalog

Before you deploy the agent, you must register the agent to Unity Catalog. Registering the agent packages it as a model in Unity Catalog. As a result, you can use Unity Catalog permissions for authorization for resources in the agent.

```python
import mlflow

mlflow.set_registry_uri("databricks-uc")

catalog_name = "test_catalog"
schema_name = "schema"
model_name = "agent_name"

model_name = catalog_name + "." + schema_name + "." + model_name
uc_model_info = mlflow.register_model(model_uri=logged_agent_info.model_uri, name=model_name)
```

See [`mlflow.register_model()`](https://mlflow.org/docs/latest/api_reference/python_api/mlflow.html#mlflow.register_model).

## <a id="additional-resources" /> Additional resources

* [Add traces to an agent](https://docs.databricks.com/aws/en/mlflow3/genai/tracing/overview.md).
* [Deploy an agent](https://docs.databricks.com/aws/en/agents/custom-agents/model-serving/deploy-agent.md).
