Getting started with AgentCore Code Interpreter by running a hello world example - Amazon Bedrock AgentCore

Amazon Bedrock AgentCore is in preview release and is subject to change.

Getting started with AgentCore Code Interpreter by running a hello world example

The following sections show how you can get started with the AgentCore Code Interpreter tool.

Prerequisites

Before using the AgentCore Code Interpreter, ensure you meet the following requirements:

  • You have an active AWS account with permissions to use Amazon Bedrock AgentCore

  • For programmatic access, you have installed and configured the AWS SDK or AWS CLI

Install the necessary packages:

# Install boto3 pip install boto3 # Configure AWS credentials aws configure # For AgentCore SDK approach, also install: pip install bedrock-agentcore

Quick start

You can quickly get started with the AgentCore Code Interpreter using either the AgentCore SDK or boto3. Both approaches allow you to create sessions and execute code.

Using boto3

This example uses the boto3 client to start a code interpreter session and run a simple hello world program:

# hello_world.py import boto3 import json code_to_execute = """ print("Hello World!!!") """ client = boto3.client("bedrock-agentcore", region_name="<Region>", endpoint_url="https://bedrock-agentcore.<Region>.amazonaws.com") session_id = client.start_code_interpreter_session( codeInterpreterIdentifier="aws.codeinterpreter.v1", name="my-code-session", sessionTimeoutSeconds=900 )["sessionId"] execute_response = client.invoke_code_interpreter( codeInterpreterIdentifier="aws.codeinterpreter.v1", sessionId=session_id, name="executeCode", arguments={ "language": "python", "code": code_to_execute } ) # Extract and print the text output from the stream for event in execute_response['stream']: if 'result' in event: result = event['result'] if 'content' in result: for content_item in result['content']: if content_item['type'] == 'text': print(content_item['text']) # Don't forget to stop the session when you're done client.stop_code_interpreter_session( codeInterpreterIdentifier="aws.codeinterpreter.v1", sessionId=session_id )
Using AgentCore SDK

This example uses the Amazon Bedrock AgentCore SDK, which provides a more streamlined interface for working with the code interpreter:

# hello_world_sdk.py from bedrock_agentcore.tools.code_interpreter_client import CodeInterpreter import json # Configure and Start the code interpreter session code_client = CodeInterpreter('<Region>') code_client.start() # Execute the hello world code response = code_client.invoke("executeCode", { "language": "python", "code": 'print("Hello World!!!")' }) # Process and print the response for event in response["stream"]: print(json.dumps(event["result"], indent=2)) # Clean up and stop the code interpreter session code_client.stop()

The Amazon Bedrock AgentCore SDK provides a simpler interface that handles session management automatically. The CodeInterpreter class creates and manages the session for you, and the invoke method makes it easy to call the various code interpreter tools.