

# Session management
<a name="code-interpreter-session-characteristics"></a>

The AgentCore Code Interpreter sessions have the following characteristics:

Session timeout  
Default: 900 seconds (15 minutes)  
Configurable: Can be adjusted when creating sessions, up to 8 hours

Session persistence  
Files and data created during a session are available throughout the session’s lifetime. When the session is terminated, the session no longer persists and the data is cleaned up.

Automatic termination  
Sessions automatically terminate after the configured timeout period

Multiple sessions  
Multiple sessions can be active simultaneously for a single code interpreter. Each session maintains its own state and environment

Retention policy  
The time to live (TTL) retention policy for the session data is 30 days.

**Topics**
+ [

## Using isolated sessions
](#code-interpreter-isolated-sessions)
+ [

# Starting a AgentCore Code Interpreter session
](code-interpreter-start-session.md)
+ [

# Executing code
](code-interpreter-execute-code.md)
+ [

# Runtime selection
](code-interpreter-runtime-selection.md)
+ [

# Stopping a AgentCore Code Interpreter session
](code-interpreter-stop-session.md)

## Using isolated sessions
<a name="code-interpreter-isolated-sessions"></a>

AgentCore Tools enable isolation of each user session to ensure secure and consistent reuse of context across multiple tool invocations. Session isolation is especially important for AI agent workloads due to their dynamic and multi-step execution patterns.

Each tool session runs in a dedicated microVM with isolated CPU, memory, and filesystem resources. This architecture guarantees that one user’s tool invocation cannot access data from another user’s session. Upon session completion, the microVM is fully terminated, and its memory is sanitized, thereby eliminating any risk of cross-session data leakage.

# Starting a AgentCore Code Interpreter session
<a name="code-interpreter-start-session"></a>

After creating a Code Interpreter, you can start a session to execute code.

**Example**  

1. To start a Code Interpreter session using the AWS CLI, use the `start-code-interpreter-session` command:

   ```
   aws bedrock-agentcore start-code-interpreter-session \
     --region <Region> \
     --code-interpreter-id "<your-code-interpreter-id>" \
     --name "my-code-session" \
     --description "My Code Interpreter session for data analysis" \
     --session-timeout-seconds 900
   ```

1. To start a Code Interpreter session using the AWS SDK for Python, use the `start_code_interpreter_session` method:

   ```
   import boto3
   
   # Initialize the boto3 client
   dp_client = boto3.client(
       'bedrock-agentcore',
       region_name="<Region>",
       endpoint_url="https://bedrock-agentcore.<Region>.amazonaws.com"
   )
   
   # Start a Code Interpreter session
   response = dp_client.start_code_interpreter_session(
       codeInterpreterIdentifier="aws.codeinterpreter.v1",
       name="sandbox-session-1",
       sessionTimeoutSeconds=3600
   )
   
   # Print the session ID
   session_id = response["sessionId"]
   print(f"Session created: {session_id}")
   ```

1. To start a new Code Interpreter session using the API, use the following call:

   ```
   # Using awscurl
   awscurl -X PUT \
     "https://bedrock-agentcore.<Region>.amazonaws.com/code-interpreters/aws.codeinterpreter.v1/sessions/start" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json" \
     --service bedrock-agentcore \
     --region <Region> \
     -d '{
       "name": "code-session-abc12345",
       "description": "code sandbox session",
       "sessionTimeoutSeconds": 900
     }'
   ```
**Note**  
You can use the managed resource ID `aws.codeinterpreter.v1` or a resource ID you get by creating a code interpreter with `CreateCodeInterpreter`.

# Executing code
<a name="code-interpreter-execute-code"></a>

Once you have started a Code Interpreter session, you can execute code in the session.

**Example**  

1. To execute code using the AWS SDK for Python, use the `invoke_code_interpreter` method:

   ```
   import boto3
   import json
   
   # Initialize the boto3 client
   dp_client = boto3.client(
       'bedrock-agentcore',
       region_name="<Region>",
       endpoint_url="https://bedrock-agentcore.<Region>.amazonaws.com"
   )
   
   # Execute code in the Code Interpreter session
   response = dp_client.invoke_code_interpreter(
       codeInterpreterIdentifier="aws.codeinterpreter.v1",
       sessionId="<your-session-id>",
       name="executeCode",
       arguments={
           "language": "python",
           "code": 'print("Hello World!!!")'
       }
   )
   
   # Process the event stream
   for event in 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"])
   ```

1. To execute code in a code interpreter session using the API, use the following call:

   ```
   # Using awscurl
   awscurl -X POST \
     "https://bedrock-agentcore.<Region>.amazonaws.com/code-interpreters/aws.codeinterpreter.v1/tools/invoke" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json" \
     -H "x-amzn-code-interpreter-session-id: your-session-id" \
     --service bedrock-agentcore \
     --region <Region> \
     -d '{
       "id": "1",
       "name": "executeCode",
       "arguments": {
         "language": "python",
         "code": "print(\"Hello, world!\")"
       }
     }'
   ```

# Runtime selection
<a name="code-interpreter-runtime-selection"></a>

For a language, you can optionally specify a particular runtime. The following table shows the supported languages and their available runtimes.


| Language | Available runtimes | 
| --- | --- | 
|  Python  |   `python`   | 
|  JavaScript  |   `nodejs` , `deno`   | 
|  TypeScript  |   `nodejs` , `deno`   | 

**Note**  
For the `nodejs` runtime, we support CommonJS (CJS) modules for JavaScript and ECMAScript (ESM) modules for TypeScript.

# Stopping a AgentCore Code Interpreter session
<a name="code-interpreter-stop-session"></a>

When you are finished using a Code Interpreter session, you should stop it to release resources and avoid unnecessary charges.

**Example**  

1. To stop a code interpreter session using the AWS CLI, use the `stop-code-interpreter-session` command:

   ```
   aws bedrock-agentcore stop-code-interpreter-session \
     --region <Region> \
     --code-interpreter-id "<your-code-interpreter-id>" \
     --session-id "<your-session-id>"
   ```

1. To stop a Code Interpreter session using the AWS SDK for Python, use the `stop_code_interpreter_session` method:

   ```
   import boto3
   
   # Initialize the boto3 client
   dp_client = boto3.client(
       'bedrock-agentcore',
       region_name="<Region>",
       endpoint_url="https://bedrock-agentcore.<Region>.amazonaws.com"
   )
   
   # Stop the Code Interpreter session
   response = dp_client.stop_code_interpreter_session(
       codeInterpreterIdentifier="aws.codeinterpreter.v1",
       sessionId="<your-session-id>"
   )
   
   print("Session stopped successfully")
   ```

1. To stop a code interpreter session using the API, use the following call:

   ```
   # Using awscurl
   awscurl -X PUT \
     "https://bedrock-agentcore.<Region>.amazonaws.com/code-interpreters/aws.codeinterpreter.v1/sessions/stop?sessionId=<your-session-id>" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json" \
     --service bedrock-agentcore \
     --region <Region>
   ```