opensi·si

Written by Dario SalvatiLast updated 2026-10-23

Build a reasoning trip-planning agent on Amazon SageMaker AI with Hugging Face vLLM

Written by Dario SalvatiLast updated 2026-10-23

In this notebook, we'll deploy Qwen/Qwen3-8B with the Hugging Face vLLM Deep Learning Container (DLC) on Amazon SageMaker, and use it to build a trip-planning agent: an assistant that reasons through multi-constraint travel requests and calls tools to check the weather, convert currencies, and find places to visit.

We'll walk through the following steps:

  • Select a reasoning and tool-calling model and a SageMaker DLC for your use case
  • Deploy the model to SageMaker with the SageMaker Python SDK ModelBuilder
  • Configure vLLM for reasoning and tool calling
  • Build an agent with Strands Agents and give it custom tools
  • Hold a multi-turn conversation where the agent reasons, calls tools, and remembers context
  • Optionally connect a Hugging Face MCP server and launch a Gradio app
  • Clean up the endpoint resources to avoid ongoing charges

For this example, you'll need AWS credentials and a SageMaker execution role. Qwen3-8B is a public model, so a Hugging Face token is optional here. Authenticated requests get more generous rate limits and faster downloads, and a token is required if you switch to a gated model.

How an agent works

An agent is a loop around a language model. The model does not just answer in one shot; it decides, step by step, what information it still needs and how to get it.

Two capabilities make this possible:

  • Reasoning: the model thinks through the problem before answering, which helps with multi-step requests such as planning a trip under several constraints.
  • Tool calling: instead of guessing a fact, the model can call a function you provide (check the weather, convert a currency, look up places) and continue with the real result.

Put together, the loop looks like this: the user asks a question, the model reasons about it and requests a tool, your code runs the tool and returns the result, and the model reasons again with that new information. This repeats until the model has everything it needs to give a final answer.

Qwen/Qwen3-8B supports both a thinking mode and native tool calling, so it can drive this loop on its own. We'll deploy it, then let Strands Agents run the loop for us.

Hugging Face vLLM and Strands Agents

vLLM is a high-throughput inference engine for large language models. The Hugging Face vLLM DLC packages it for SageMaker with current transformers and huggingface_hub, and exposes an OpenAI-compatible API (including tool calling and reasoning parsing) through simple environment variables. Its multimodal sibling, vLLM-Omni, serves tasks like text-to-speech or image generation; here we serve a text model, so we use the plain vLLM image.

Strands Agents is a lightweight agent framework with a native SageMaker integration. Instead of hand-writing the tool-calling loop, we point Strands at our endpoint and it orchestrates the reasoning, tool calls, and results for us.

Setup

To run this example, we'll install the SageMaker Python SDK for model deployment, huggingface_hub for authentication, strands-agents (with its SageMaker integration) for the agent, and gradio for the optional interactive app.

%pip install -q "sagemaker>=3" huggingface_hub "strands-agents[sagemaker]>=1.48.0" "gradio>=5"

We are going to need:

  • An HF_TOKEN: used to download the model from Hugging Face. Optional for the public Qwen3-8B; authenticated requests get more generous rate limits and faster downloads (and a token is required for gated models).
  • A SageMaker execution role: used to pull the DLC from ECR and deploy the model to SageMaker.

Let's start by setting up the token and the execution role.

from huggingface_hub import get_token

HF_TOKEN = get_token()

if HF_TOKEN:
    print("HF_TOKEN loaded")
else:
    print(
        "No HF_TOKEN found. Public Qwen3-8B downloads still work. Run "
        "huggingface_hub.notebook_login() to authenticate for higher rate "
        "limits or to access gated models."
    )

Production tip: avoid pasting long-lived tokens into notebooks or committing them. For production, keep the token in a vault such as AWS Secrets Manager and load it at deploy time:

import json
import boto3

secret = boto3.client("secretsmanager").get_secret_value(SecretId="hf-token")
HF_TOKEN = json.loads(secret["SecretString"])["HF_TOKEN"]

To avoid handing the token to the endpoint at all, pre-stage the model weights in S3 and point the container at them, so no Hub token is needed at runtime.

import os

import boto3
from sagemaker.core.helper.session_helper import Session, get_execution_role

REGION = boto3.Session().region_name or os.environ.get("AWS_REGION", "us-east-1")
boto_sess = boto3.Session(region_name=REGION)
sess = Session(boto_session=boto_sess)

try:
    role = get_execution_role(sagemaker_session=sess)
    print(f"Role extracted from execution role: {role}")
except Exception:
    role_name = "sagemaker_execution_role"
    iam_client = boto_sess.client("iam")
    role = iam_client.get_role(RoleName=role_name)["Role"]["Arn"]
    print(f"Role extracted from iam client: {role}")

Choosing a model and a DLC

There are many open LLMs on the Hugging Face Hub. For an agent we want strong instruction-following, reasoning, and tool-calling, while staying small enough to run on a single GPU.

We'll use Qwen/Qwen3-8B and serve it with the Hugging Face vLLM DLC. Rather than hardcoding the container URI, we resolve it with the SageMaker SDK's image_uris.retrieve helper, which returns the right image for the chosen region and instance. You can also browse the available images on the AWS Available Images page.

We'll target an ml.g5.xlarge instance (a single NVIDIA A10G 24GB GPU). For higher concurrency or larger models, consider larger GPU instance types such as ml.g5.2xlarge or ml.g6e.*.

from time import strftime

from sagemaker.core.image_uris import retrieve

MODEL_ID = "Qwen/Qwen3-8B"
INSTANCE_TYPE = "ml.g5.xlarge"
IMAGE_URI = retrieve(
    "huggingface-vllm",
    region=REGION,
    image_scope="inference",
    instance_type=INSTANCE_TYPE,
)

RESOURCE_SUFFIX = strftime("%Y%m%d-%H%M%S")
MODEL_NAME = f"trip-planner-agent-model-{RESOURCE_SUFFIX}"
ENDPOINT_NAME = f"trip-planner-agent-endpoint-{RESOURCE_SUFFIX}"

print(IMAGE_URI)

Configuring vLLM for reasoning and tool calling

The vLLM container is configured entirely through environment variables. Any vLLM server flag can be passed by uppercasing it, replacing dashes with underscores, and prefixing it with SM_VLLM_ (for example --max-model-len becomes SM_VLLM_MAX_MODEL_LEN).

Three of these variables are what turn a plain text model into an agent backend:

  • SM_VLLM_ENABLE_AUTO_TOOL_CHOICE=true lets the model decide when to call a tool.
  • SM_VLLM_TOOL_CALL_PARSER=hermes parses Qwen3's tool calls into the OpenAI format.
  • SM_VLLM_REASONING_PARSER=qwen3 separates the model's thinking from its final answer.

We also set SM_VLLM_HOST=0.0.0.0, which is required so the container passes the SageMaker health check. ModelBuilder sets HF_MODEL_ID for us, so we don't repeat the model id here.

env_vars = {
    "SM_VLLM_HOST": "0.0.0.0",  # Bind to all interfaces so the health check passes
    "SM_VLLM_MAX_MODEL_LEN": "16384",  # Context length; bounds KV cache on a single A10G
    "SM_VLLM_GPU_MEMORY_UTILIZATION": "0.9",
    "SM_VLLM_ENABLE_AUTO_TOOL_CHOICE": "true",  # Let the model call tools
    "SM_VLLM_TOOL_CALL_PARSER": "hermes",  # Parse Qwen3 tool calls
    "SM_VLLM_REASONING_PARSER": "qwen3",  # Separate reasoning from the answer
}

# Only pass the token when present; SageMaker rejects non-string env values.
if HF_TOKEN:
    env_vars["HF_TOKEN"] = HF_TOKEN

Deploy the model

The SageMaker Python SDK v3 deploys models with ModelBuilder: we describe the model, the serving container, and the instance, then call build and deploy. There is no need to assemble model, endpoint-config, and endpoint resources by hand.

One detail worth calling out: this image is built on CUDA 13 (cu130 in the tag). vLLM images with CUDA 13 or newer require the al2-ami-sagemaker-inference-gpu-3-1 inference AMI, which we pass to deploy. Without it the container fails to start before any logs are produced.

from sagemaker.serve import ModelBuilder, ModelServer

model_builder = ModelBuilder(
    model=MODEL_ID,
    role_arn=role,
    sagemaker_session=sess,
    instance_type=INSTANCE_TYPE,
    image_uri=IMAGE_URI,
    model_server=ModelServer.VLLM,
    env_vars=env_vars,
)

built_model = model_builder.build(model_name=MODEL_NAME)

model_builder.deploy(
    endpoint_name=ENDPOINT_NAME,
    initial_instance_count=1,
    instance_type=INSTANCE_TYPE,
    inference_ami_version="al2-ami-sagemaker-inference-gpu-3-1",
    container_timeout_in_seconds=900,
    wait=True,
)

A first look: reasoning and tool calls

Before bringing in the agent framework, let's send a single request straight to the endpoint to see what the model returns. SageMaker requests go through the /invocations route; we use CustomAttributes to forward them to vLLM's OpenAI-compatible Chat Completions API.

We pass a get_weather tool definition and let the model decide whether to call it (tool_choice="auto"). The response separates the model's reasoning (reasoning_content) from any tool calls it wants to make (tool_calls).

import json

runtime = boto_sess.client("sagemaker-runtime")

weather_tool = {
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get the weather forecast for a city on a given date.",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {"type": "string", "description": "City name."},
                "date": {"type": "string", "description": "Date in YYYY-MM-DD format."},
            },
            "required": ["city", "date"],
        },
    },
}

response = runtime.invoke_endpoint(
    EndpointName=ENDPOINT_NAME,
    ContentType="application/json",
    Body=json.dumps(
        {
            "model": MODEL_ID,
            "messages": [{"role": "user", "content": "What should I pack for Lisbon on 2026-10-12?"}],
            "tools": [weather_tool],
            "tool_choice": "auto",
        }
    ),
    CustomAttributes="route=/v1/chat/completions",
)

message = json.loads(response["Body"].read())["choices"][0]["message"]
print("Reasoning:\n", message.get("reasoning_content"), "\n")
print("Tool calls:\n", json.dumps(message.get("tool_calls"), indent=2))