You will open a safe cloud computer, run a line of Python in it, read the result, and close it. Two ways: the simple SDK client, or raw boto3 for more control. Terms: session = one open run of the sandbox (default 15 minutes); aws.codeinterpreter.v1 = the built-in interpreter; executeCode = the action that runs your code; stream = the result arrives piece by piece.
from bedrock_agentcore.tools.code_interpreter_client import CodeInterpreter import json code_client = CodeInterpreter('<Region>') # e.g. 'us-west-2' code_client.start() # open a session try: response = code_client.invoke("executeCode", { "language": "python", "code": 'print("Hello World!!!")' }) for event in response["stream"]: print(json.dumps(event["result"], indent=2)) finally: code_client.stop() # always clean up
import boto3 client = boto3.client("bedrock-agentcore", region_name="<Region>") s = client.start_code_interpreter_session( codeInterpreterIdentifier="aws.codeinterpreter.v1", # the built-in interpreter name="my-code-session", sessionTimeoutSeconds=900) # 900s = 15 minutes session_id = s["sessionId"] try: r = client.invoke_code_interpreter( codeInterpreterIdentifier="aws.codeinterpreter.v1", sessionId=session_id, name="executeCode", arguments={"language": "python", "code": 'print("Hello World!!!")'}) for event in r["stream"]: if "result" in event: for c in event["result"].get("content", []): if c["type"] == "text": print(c["text"]) finally: client.stop_code_interpreter_session( codeInterpreterIdentifier="aws.codeinterpreter.v1", sessionId=session_id)