Build agent workflows using Openai GPT OSS with Amazon Sagemaker AI and Amazon Bedrock Ageentcore

Machine Learning


Openai has released two open weight models. gpt-oss-120b (117 billion parameters) and gpt-oss-20b (21 billion parameters), both are built with a mixture of expert (MOE) designs and 128K context windows. These models are the leading open source models, according to artificial analysis benchmarks, and are excellent at inference and agent workflows. Amazon Sagemaker AI allows you to fine-tune or customize your model and deploy it in your framework of choice through a fully managed service. Amazon Sagemaker inference gives you the flexibility to bring your own inference code and framework without having to build and maintain your own cluster.

Large-scale language models (LLMs) are excellent at understanding languages ​​and generating content, but building real agent applications requires complex workflow management, tool invocation capabilities, and context management. By dividing complex systems into specialized components, multi-agent architectures address these challenges, but introduce new complexities in agent coordination, memory management, and workflow orchestration.

This post shows how to unfold gpt-oss-20b The model to Sagemaker manages endpoints and provides examples of practical Stock Analyzer Agent Assistant using Langgraph. It is a powerful graph-based framework that handles state management, coordinated workflows, and persistent memory systems. You then deploy the agent to Amazon Bedrock AgentCore. It is an integrated orchestration layer that abstracts infrastructure and allows you to safely deploy and operate AI agents at scale.

Solution overview

This solution builds an agent stock analyzer with the following key components:

  • GPT OSS 20B model deployed to Sagemaker endpoints using VLLM, LLM's open source serving framework
  • Lang graphs for building multi-agent orchestration frameworks
  • Amazon Bedrock AgentCore deploys agents

The following diagram illustrates the solution architecture.

This architecture illustrates a multi-agent workflow hosted by the Amazon Bedrock AgentCore runtime running on AWS. Users submit queries that are processed by the pipelines of the DATA Gathering Agent, Stock Performance Analyzer Agent, and Stock Report Generation Agent, respectively, which are responsible for a clear part of the stock valuation process.

These agents collaborate within Amazon Bedrock's agent core runtime and invoke the GPT OSS model hosted by Sagemaker AI if language understanding or generation is required. This model processes input and returns structured output that informs agent actions, allowing for fully serverless, modular, and scalable agent systems using the open source model.

Prerequisites

  1. Make sure that your G6E instance requires quotas to deploy the model. If not, please request a quota here.
  2. If you are working with Amazon Sagemaker Studio for the first time, you must first create a Sagemaker domain.
  3. Make sure that the IAM role requires permission to deploy the Sagemaker model and endpoints. For more information, see How Amazon Sagemaker AI Works with IAM with Sagemaker Developer Guide.

Deploying the gpt-soss model to sage maker inference

Customers who want to customize their models and frameworks can deploy using a server-full deployment, but need access to GPU, serving framework, load balancer and infrastructure setup. Sagemaker AI provides a fully managed hosting platform to handle infrastructure provisioning with the required drivers, download models, and deploy them. OpenAI's GPT-OSS model is launched with a 4-bit quantization scheme (MXFP4) that allows for fast inference while keeping resource usage low. These models can run on P5 (H100), P6 (H200), and P4 (A100) and G6E (L40) instances. The GPT-OSS model is a sparse MOE architecture with 128 experts (120B) or 32 experts (20B). Using MXFP4 for MOE weights alone reduces the model size to 63 GB (120B) and 14 GB (20B) and makes it possible to run on a single H100 GPU.

Effective deployment of these models requires a powerful serving framework like VLLM. To deploy the model, build a VLLM container with the latest version that supports the GPT OSS model of Sagemaker AI.

You can use the following Docker files and scripts to build containers and push them to your local Amazon Elastic Container Registry (Amazon ECR). The recommended approach is to do this directly from Amazon Sagemaker Studio. It provides a managed JupyterLab environment with AWS CLI access that allows you to build and push images to ECR as part of your SageMaker workflow. Alternatively, you can perform the same steps on your Amazon Elastic Compute Cloud (Amazon EC2) instance with Docker installed.

After building the container into Amazon ECR and pressing it, you can open Amazon Sagemaker Studio by going to the SageMaker AI console, as shown in the following screenshot.

You can then create a Jupyter space or start JupyterLab with the existing space to run the notebook.

Clone the following notebook and run “Option 3: Extract from HF using BYOC”. Update the required parameters, such as inference images in notebooks with container images. It also provides the required environment variables, as shown in the following code:

inference_image  f"{account_id}.dkr.ecr.{region}.amazonaws.com/vllm:v0.10.0-gpt-oss"
instance_type  "ml.g6e.4xlarge"
num_gpu  1
model_name  sagemakerutilsname_from_base("model-byoc")
endpoint_name  model_name
inference_component_name  f"ic-{model_name}"
config  {
"OPTION_MODEL": "openai/gpt-oss-20b",
"OPTION_SERVED_MODEL_NAME": "model",
"OPTION_TENSOR_PARALLEL_SIZE": jsondumps(num_gpu),
"OPTION_ASYNC_SCHEDULING": "true",
}

After you set up your deployment configuration, you can deploy it to Sagemaker AI using the following code:

from sagemaker.compute_resource_requirements.resource_requirements import ResourceRequirements

lmi_model = sagemaker.Model(
    image_uri=inference_image,
    env=config,
    role=role,
    name=model_name,
)

lmi_model.deploy(
    initial_instance_count=1,
    instance_type=instance_type,
    container_startup_health_check_timeout=600,
    endpoint_name=endpoint_name,
    endpoint_type=sagemaker.enums.EndpointType.INFERENCE_COMPONENT_BASED,
    inference_component_name=inference_component_name,
    resources=ResourceRequirements(requests={"num_accelerators": num_gpu, "memory": 1024*5, "copies": 1,}),
)

You can run an inference example.

payload={
    "messages": [
        {"role": "user", "content": "Name popular places to visit in London?"}
    ],
}
res = llm.predict(payload)
print("-----\n" + res["choices"][0]["message"]["content"] + "\n-----\n")
print(res["usage"])

-----
Here are some of the must‑see spots in London — a mix of iconic landmarks, world‑class museums, and vibrant neighborhoods:

| # | Place | Why It’s Popular |
|---|-------|------------------|
| 1 | **Buckingham Palace** | The Queen’s official London residence – watch the Changing of the Guard. |
| 2 | **The Tower of London & Tower Bridge** | Historic castle, Crown Jewels, and the iconic bridge with glass floors. |
| 3 | **The British Museum** | World‑famous collection from the Rosetta Stone to Egyptian mummies (free entry). |
| 4 | **The Houses of Parliament & Big Ben** | The classic symbol of London’s politics and architecture. |
| 5 | **The National Gallery (Tate Britain)** | Home to masterpieces from Van Gogh to Turner. |
| 6 | **Buckinghamshire Gardens (Kew Gardens)** | Stunning botanical gardens with a glasshouse and the Horniman Insect Zoo. |
| 7 | **Camden Market** | Eclectic stalls, street food, music and vintage fashion. |
| 8 | **Covent Garden** | Lively piazza with street performers, boutique shops, and the Royal Opera House. |
| 9 | **West End Theatres** | Theatre district famous for grand productions (musicals, dramas). |
|10 | **The Shard** | Skyscraper with panoramic 360° views of London. |
|11 | **St. Paul’s Cathedral** | Massive dome, stunning interior and a climb up the Whispering Gallery. |
|12 | **The Tate Modern** | Contemporary art museum set in a former power station. |
|13 | **The Victoria & Albert Museum** | Design and fashion, costume, and jewelry collections. |
|14 | **Hyde Park & Kensington Gardens** | Huge green spaces with Serpentine Lake, Speaker’s Corner and Speakers' Corner. |
|15 | **Oxford Street & Regent Street** | Prime shopping streets for fashion, flagship stores, and historic architecture. |

These spots cover history, culture, shopping, and leisure—perfect for a first visit or a weekend escape in London!
-----

Use Langgraph to build a Stock Analyzer Agent

Use Langgraph to adjust the workflow to analyze multi-agent systems. The Jupyter notebook for your code can be found in this GitHub repository. The system consists of three specialized tools that work together to comprehensively analyze stocks.

  • gather_stock_data The tool collects comprehensive stock data for specific ticker symbols, including current prices, historical performance, financial indicators, and market data. Returns formatted information covering price history, company foundations, transaction metrics, and recent news headlines.
  • analyze_stock_performance The tool performs detailed technical and basic analysis of stock price data and calculates metrics such as price trends, volatility, and overall investment scores. Evaluate multiple factors including P/E ratio, profit margin and dividend yield to provide a comprehensive performance analysis
  • generate_stock_reportThe tool creates professional PDF reports from collected inventory data and analysis and automatically uploads them to Amazon S3 using organized date-based folders.

Local testing allows you to use a simplified version of the system by importing the required functionality from a local script. for example:

from langgraph_stock_local import langgraph_stock_sagemaker
# Test the agent locally
result = langgraph_stock_sagemaker({
    "prompt": "Analyze SIM_STOCK Stock for Investment purposes."
})
print(result)

In this way, before deploying the agent on a scalable platform, iterate through the agent logic quickly to ensure that each component works correctly and that the overall workflow produces the expected results for different types of inventory.

Deploy to Amazon Bedrock AgentCore

After developing and testing the Langgraph framework locally, you can deploy it to the Amazon Bedrock Agentcore runtime. Amazon Bedrock Agentcore handles heavy objects that abstract container orchestration, session management, scalability, and infrastructure management. Provides a persistent execution environment where multiple calls can maintain the agent's state.

Before you can deploy the Stock Analyzer Agent to the Amazon Bedrock AgentCore runtime, you must create an AWS Identity and Access Management IAM role with the appropriate permissions. This role allows Amazon Bedrock AgentCore to invoke SageMaker endpoints for inference of the GPT-OSS model, manage the ECR repository for storing container images, write Amazon CloudWatch logs for monitoring and debugging, access Amazon Bedrock Agemcore Workload Services for runtime operations, and send telemetry data to AWS Xray and Cloudwatch. See the following code:

from create_agentcore_role import create_bedrock_agentcore_role
role_arn = create_bedrock_agentcore_role(
    role_name="MyStockAnalyzerRole",
    sagemaker_endpoint_name="your-endpoint-name",
    region="us-west-2"
)

After you create a role, you can deploy the agent using the Amazon Bedrock AgentCore Starter Toolkit. The toolkit simplifies the deployment process by packaging code, creating the required container images, and configuring the runtime environment.

from bedrock_agentcore_starter_toolkit import Runtime
agentcore_runtime = Runtime()
# Configure the agent
response = agentcore_runtime.configure(
    entrypoint="langgraph_stock_sagemaker_gpt_oss.py",
    execution_role=role_arn,
    auto_create_ecr=True,
    requirements_file="requirements.txt",
    region="us-west-2",
    agent_name="stock_analyzer_agent"
)
# Deploy to the cloud
launch_result = agentcore_runtime.launch(local=False, local_build=False)

When you are using BedrockAgentCoreAppAutomatically create an HTTP server that listens to port 8080 and implements what you need /invocations An endpoint for handling agent requirements implements/ping Handles health check endpoints (very important for asynchronous agents), the appropriate content type and response format, and manages error handling according to AWS standards.

After deploying to the Amazon Bedrock AgentCore runtime, you can see the status show as follows Ready Amazon Bedrock Agentcore Console.

Call the agent

Once you have created the agent, you need to set up the call entry point for the agent. The Amazon AgentCore runtime decorates the calling portion of the agent. @app.entrypoint Use the decorator and it as the runtime entry point. After you deploy the agent to the Amazon AgentCore runtime, you can invoke it using the AWS SDK.

import boto3
import json
agentcore_client = boto3.client('bedrock-agentcore', region_name="us-west-2")
response = agentcore_client.invoke_agent_runtime(
    agentRuntimeArn=launch_result.agent_arn,
    qualifier="DEFAULT",
    payload=json.dumps({
        "prompt": "Analyze SIM_STOCK for investment purposes"
    })
)

After calling the Stock Analyzer Agent via the Amazon Bedrock AgentCore runtime, you must parse and format the response for a clear presentation. The response process includes the following steps:

  1. Decodes a byte stream from Amazon Bedrock AgentCore into readable text.
  2. Analyze JSON responses that include a complete inventory analysis.
  3. Extract three main sections using regular expression pattern matching.
    1. Inventory data collection section: Extract core stock information including symbols, company details, current pricing, market metrics, financial ratios, transaction data, and recent news headlines.
    2. Performance Analysis Section: Analyze technical indicators, basic indicators, and volatility measures to generate comprehensive stock analysis.
    3. Inventory Report Generation Section: Generate a detailed PDF report using all inventory technology analysis.

The system also includes error handling that gracefully handles JSON parsing errors, and returns to the plain text display if structured analysis fails, providing debugging information to troubleshoot analysis problems in stock analysis responses.

stock_analysis = parse_bedrock_agentcore_stock_response(invoke_response)

This formatted output makes it easy to review the agent's decision-making process, present professional stock analysis results to stakeholders, and complete end-to-end workflows from model deployment to meaningful business output.

STOCK DATA GATHERING REPORT:
================================
Stock Symbol: SIM_STOCK
Company Name: Simulated Stock Inc.
Sector: SIM_SECTOR
Industry: SIM INDUSTRY
CURRENT MARKET DATA:
- Current Price: $29.31
- Market Cap: $3,958
- 52-Week High: $29.18
- 52-Week Low: $16.80
- YTD Return: 1.30%
- Volatility (Annualized): 32.22%
FINANCIAL METRICS:
- P/E Ratio: 44.80
- Forward P/E: 47.59
- Price-to-Book: 11.75
- Dividend Yield: 0.46%
- Revenue (TTM): $4,988
- Profit Margin: 24.30%

STOCK PERFORMANCE ANALYSIS:
===============================
Stock: SIM_STOCK | Current Price: $29.31
TECHNICAL ANALYSIS:
- Price Trend: SLIGHT UPTREND
- YTD Performance: 1.03%
- Technical Score: 3/5
FUNDAMENTAL ANALYSIS:
- P/E Ratio: 34.80
- Profit Margin: 24.30%
- Dividend Yield: 0.46%
- Beta: 1.165
- Fundamental Score: 3/5
STOCK REPORT GENERATION:
===============================
Stock: SIM_STOCK 
Sector: SIM_INDUSTRY
Current Price: $29.78
REPORT SUMMARY:
- Technical Analysis: 8.33% YTD performance
- Report Type: Comprehensive stock analysis for informational purposes
- Generated: 2025-09-04 23:11:55
PDF report uploaded to S3: s3://amzn-s3-demo-bucket/2025/09/04/SIM_STOCK_Stock_Report_20250904_231155.pdf
REPORT CONTENTS:
• Executive Summary with key metrics
• Detailed market data and financial metrics
• Technical and fundamental analysis
• Professional formatting for documentation

cleaning

You can delete the SageMaker endpoint by running the following cells in the same notebook to avoid acquiring costs after testing.

sessdelete_inference_component(inference_component_name)
sessdelete_endpoint(endpoint_name)
sessdelete_endpoint_config(endpoint_name)
sessdelete_model(model_name)

You can also delete an Amazon Bedrock AgentCore resource using the following command:

runtime_delete_response  agentcore_control_clientdelete_agent_runtime(
agentRuntimeIdlaunch_resultagent_id
)
response  ecr_clientdelete_repository(
repositoryNamelaunch_resultecr_urisplit('/')[1],
force
)

Conclusion

In this post, we built an end-to-end solution for deploying OpenAI's open weight model on a single G6E (L40S) GPU, created a multi-agent stock analysis system with Langgraph, and deployed seamlessly with Amazon Bedrock Agemcore. This implementation shows that efficient serving frameworks such as VLLM allow organizations to use powerful open source LLM in cost-effective use. Beyond technical implementation, this workflow can be enhanced to deliver important business value, such as reducing inventory analysis processing time and increasing analyst productivity by automating daily inventory valuations. Additionally, by releasing analysts from repeated tasks, organizations can redirect skilled professionals to complex cases and relationship building activities that drive business growth.

I recommend trying the code samples and repeating the agent workflow to meet your use case.


About the author

Vivek Gangasani The world leader in Sagemaker's inference is Genai Specialist Solutions Architect. He is driving markets to markets (GTM) and outbound product strategies for Sagemaker reasoning. He also helps enterprises and startups deploy, manage and expand Genai models with Sage Makers and GPUs. Currently he focuses on developing strategies and solutions to optimize inference performance and GPU efficiency to host large language models. During his free time, Vivek will hike, watch movies and try a variety of dishes.

Surya Kali He is a senior-generated AI data scientist at AWS, specializing in developing solutions that utilize the latest basic models. He has extensive experience working together on advanced language models such as the Deepseek-R1, the Llama family, and Qwen to focus on fine-tuning and optimizing specific scientific applications. His expertise extends to implementing efficient training pipelines and deployment strategies using AWS Sagemaker, allowing for scaling basic models from development to production. He works with his clients to design and implement generated AI solutions and navigate model selection, fine-tuning approaches, and deployment strategies to achieve optimal performance for a particular use case.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *