Sub topic 3
Foundation Model APIs and model serving endpoints
This chapter assumes you can write basic Python and have used a Databricks notebook. It also assumes you know what a chat message and a prompt are. You will learn how Databricks exposes language models as HTTP endpoints and how to choose between the available kinds.
What a serving endpoint is
A model serving endpoint is a managed, named HTTP service that runs a model for you. You send it a request and it returns a response. Databricks handles the compute, scaling, and authentication.
Everything you call in Mosaic AI goes through this one idea. Chat models, embedding models, and your own deployed agents are all endpoints. They differ in what they run, but you call them in a similar way.
Foundation Model APIs
Foundation Model APIs give you access to hosted foundation models. These are large pretrained models, such as chat and embedding models, that you use without training or hosting them yourself. They come in two modes.
| Mode | How you pay | Best for |
|---|---|---|
| Pay-per-token | Per token processed | Prototyping, light or unpredictable traffic |
| Provisioned throughput | Reserved capacity per hour | Production traffic that needs steady performance |
Pay-per-token endpoints are ready to use in most workspaces. Provisioned throughput gives you dedicated capacity, so latency stays predictable under load. Which models and regions are available changes over time, so check your workspace's Serving page.
Three kinds of endpoint
Besides Foundation Model APIs, a workspace can hold other endpoints:
- Foundation model endpoints serve models that Databricks hosts.
- External model endpoints act as a governed proxy to a model hosted by another provider. Your code calls Databricks, and Databricks forwards the request.
- Custom endpoints serve models or agents you have registered yourself. Later chapters deploy an agent this way.
Because the interface is the same, you can switch the model behind an endpoint name without rewriting your application logic.
Finding and listing endpoints
You can list the endpoints you can see with the Databricks SDK. This is a quick way to find the exact name to use later.
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
for endpoint in w.serving_endpoints.list():
print(endpoint.name)WorkspaceClient() reads credentials from the notebook context, or from environment variables and a configuration profile when you run outside Databricks. You do not paste tokens into code.
Calling an endpoint through the SDK
The SDK can return an OpenAI-compatible client that is already authenticated for your workspace. The next chapter covers it in detail. Here is the short version.
from databricks.sdk import WorkspaceClient
ENDPOINT_NAME = "<your-chat-endpoint-name>"
w = WorkspaceClient()
client = w.serving_endpoints.get_open_ai_client()
response = client.chat.completions.create(
model=ENDPOINT_NAME,
messages=[{"role": "user", "content": "What is a serving endpoint?"}],
max_tokens=100,
)
print(response.choices[0].message.content)Notice that model holds the endpoint name, not a provider's model identifier. The endpoint decides which model answers.
Common errors
- Endpoint not found. The name has a typo, or the endpoint is not in the workspace you authenticated against. List endpoints and copy the name.
- Permission denied. Your user or service principal needs the permission to query that endpoint. Ask the endpoint owner to grant it.
- Rate limit or capacity errors. Pay-per-token endpoints have limits. Retry with backoff, or move steady production load to provisioned throughput.
- Wrong endpoint type. An embedding endpoint rejects chat requests, and the reverse. Check the endpoint's task type.
Exercise
Your prototype uses a pay-per-token chat endpoint. Traffic is now steady and high, and response times vary during busy periods. What should you consider, and what do you need to change in your calling code?
Show answer
Consider provisioned throughput, which reserves capacity and keeps latency steadier. If you serve the model on a new endpoint, change only the endpoint name you pass as model. The calling code otherwise stays the same.
Exercise
A call fails with an error saying the endpoint accepts embedding requests only. What is the likely cause?
Show answer
You sent a chat completions request to an embedding endpoint. Check the endpoint's task type and use a chat endpoint for chat requests.