• Documentation
  • Pricing
© 2026 Serverless, Inc. All rights reserved.

Framework

  • Overview
  • Documentation
  • Plugins360
  • Pricing

Learn

  • Blog
  • GuidesUpdated
  • Examples240
  • Courses

Resources

  • Support
  • Security
  • Trust Center
  • Status

Community

  • Slack
  • GitHub47k
  • Forum
  • Meetups

Company

  • About
  • Careers
  • Contact
  • Partners

Legal

  • Terms of Service
  • Privacy Policy
  • Trademark
  • DMCA
Serverless, inc.

Bedrock AgentCore: LangGraph Agent with Custom Code Interpreter (Python)

by

LangGraph agent using a custom AgentCore Code Interpreter with PUBLIC network mode.

  1. Bedrock AgentCore: LangGraph Agent with Custom Code Interpreter (Python)

LangGraph Custom Code Interpreter Example

This example demonstrates using a custom code interpreter with PUBLIC network mode, allowing code to access external APIs and internet resources.

Features

  • Custom Code Interpreter: Creates a CodeInterpreterCustom CloudFormation resource
  • PUBLIC Network Mode: Code can access external APIs (unlike default SANDBOX mode)
  • Custom Tool Wrapper: Uses CodeInterpreter directly with custom identifier
  • Network Validation: Test script verifies PUBLIC mode by fetching external data

Network Mode Comparison

ModeNetwork AccessUse Case
SANDBOX (default)NoneMaximum security, local computation
PUBLIC (this example)InternetFetch APIs, download data
VPCPrivate VPCAccess internal resources

Project Structure

langgraph-code-interpreter-custom/
├── serverless.yml     # Custom interpreter + agent configuration
├── agent.py           # LangGraph agent with custom interpreter
├── Dockerfile         # Container definition
├── pyproject.toml     # Python dependencies
├── test-invoke.py     # Validation script (tests PUBLIC mode)
└── README.md          # This file

Prerequisites

  • AWS account with Bedrock model access
  • Docker installed
  • Serverless Framework v4+
  • Python 3.14+

Deployment

  1. Deploy the agent:
serverless deploy
  1. Note the runtime endpoint URL from the output (the ARN is embedded in the URL path):
ai:
  codeInterpreters: N/A
  agents:
    codeAgent: https://bedrock-agentcore.us-east-1.amazonaws.com/runtimes/arn:aws:bedrock-agentcore:us-east-1:123456789012:runtime/.../invocations

Testing

Run the validation script to verify PUBLIC network mode works:

export RUNTIME_ARN="arn:aws:bedrock-agentcore:us-east-1:123456789012:runtime/..."
python test-invoke.py

The test asks the agent to fetch data from GitHub's public API. This would fail in SANDBOX mode but succeeds in PUBLIC mode.

How It Works

Custom Interpreter Configuration

ai:
  codeInterpreters:
    publicInterpreter:
      description: Code interpreter with public internet access
      network:
        mode: PUBLIC # Enables external network access

Agent Code

from bedrock_agentcore.tools.code_interpreter_client import CodeInterpreter

# Get custom interpreter ID from environment
CUSTOM_INTERPRETER_ID = os.environ.get("CUSTOM_INTERPRETER_ID")

# Create client with custom identifier
code_interpreter = CodeInterpreter(region=AWS_REGION)
code_interpreter.start(identifier=CUSTOM_INTERPRETER_ID)  # Uses PUBLIC mode

# Execute code (can access external APIs)
response = code_interpreter.invoke(
    method="executeCode",
    params={"code": code, "language": "python"}
)

code_interpreter.stop()

IAM Permissions

The agent's role needs permission to use the custom interpreter:

role:
  statements:
    - Effect: Allow
      Action:
        - bedrock-agentcore:InvokeCodeInterpreter
      Resource: !GetAtt PublicInterpreterCodeInterpreter.CodeInterpreterArn

Example Prompts

# Fetch external API data (requires PUBLIC mode)
"Fetch the current Bitcoin price from a public API"

# Download and process data
"Download the iris dataset from scikit-learn and show summary statistics"

# Web scraping
"Fetch the HTML from example.com and count the number of paragraphs"

Cleanup

Remove the deployed resources:

serverless remove

Related Examples

  • langgraph-code-interpreter - Basic example with default interpreter (SANDBOX)

Contents

  • LangGraph Custom Code Interpreter Example
  • Features
  • Network Mode Comparison
  • Project Structure
  • Prerequisites
  • Deployment
  • Testing
  • How It Works
  • Custom Interpreter Configuration
  • Agent Code
  • IAM Permissions
  • Example Prompts
  • Cleanup
  • Related Examples

Related

GuidesPluginsExamplesSlack CommunitySupport