# Multiple Sessions

AgentGPT allows simultaneous execution of independent workflows (*sessions*) while maintaining:

* ⚖️ **Resource Isolation**: Separate gas budgets, AI models, and chains per session
* 🔗 **Cross-Session Coordination**: State synchronization via ERC-7645 Session Tokens
* 🧠 **Priority Management**: GPT-4 dynamically allocates compute based on ROI predictions

***

#### **Session Architecture**

```mermaid
flowchart TD  
    Master[Master Session] -->|ERC-7645 Token| SessionA  
    Master -->|ERC-7645 Token| SessionB  
    SessionA[DeFi Arbitrage] -->|Profit| Treasury  
    SessionB[NFT Minting] -->|Revenue| Treasury  
    style Master fill:#8f8,stroke:#333  
```

**Key Components**:

* **Central Controller Contract**: Manages session spawn/termination (EVM)
* **Cross-Chain State Oracle**: Syncs session status across 15+ chains
* **AI Load Balancer**: GPT-4 assigns GPU/CPU resources between sessions

***

#### **Configuration**

**Multi-Session Setup (YAML)**

```yaml
sessions:  
  - id: arbitrage_eth_poly  
    chains: [1, 137]  
    type: DEFI  
    config:  
      models:  
        price_analysis: "gpt-4-defi-v3"  
        risk_model: "gpt-4-risklab"  
      gas:  
        max_per_tx: 0.05 ETH  
        priority: "high"  
  - id: nft_generator  
    chains: [42161, 10]  
    type: NFT  
    config:  
      mint_rate: 50/min  
      royalties:  
        enforce: "ERC-4519"  
```

**Initiate from CLI**

```bash
agentgpt session spawn \  
    --config ./multi_session.yaml \  
    --gas-token AGPT \  
    --zk-circuit session_v2.zk  
```

***

#### **Cross-Session Coordination**

**Ethereum Smart Contract**

```solidity
// contracts/SessionManager.sol  
function createChildSession(  
    bytes32 parentSessionId,  
    SessionConfig memory config  
) public returns (bytes32 childId) {  
    childId = keccak256(abi.encode(parentSessionId, config));  
    sessions[childId] = Session({  
        parent: parentSessionId,  
        status: SessionStatus.ACTIVE  
    });  
    emit SessionCreated(childId, config.chainId);  
}  
```

**Synchronization Rules**

| Rule Type            | Enforcement Method    | Example                          |
| -------------------- | --------------------- | -------------------------------- |
| **State Sharing**    | ERC-7645 State Tokens | LP positions across DEXes        |
| **Resource Fencing** | Chainlink Keepers     | Prevent overwrite on shared NFTs |
| **Event Forwarding** | Interplanetary PubSub | NFT mint → Secondary listing     |

***

#### **Monitoring & Control**

**Real-Time Dashboard**

```bash
agentgpt sessions list --format table  
```

**Output**:

```
ACTIVE SESSIONS  
┌───────────────┬──────────┬─────────────┬───────────┐  
│ Session ID     │ Chain    │ Type       │ Profit    │  
├───────────────┼──────────┼────────────┼───────────┤  
│ arb_eth_poly   │ Ethereum │ DeFi      │ +12.4 ETH │  
│ nft_gen_v1     │ Arbitrum │ NFT       │ +2.1 ETH  │  
│ cross_swap     │ Base     │ Bridge    │ +0.85 ETH │  
└───────────────┴──────────┴─────────────┴───────────┘  
Total GPU Utilization: 86% | Failed Sessions: 0  
```

**Session Intervention**

```bash
# Pause/Restart Session  
agentgpt session pause 0x... --chain polygon  
agentgpt session resume 0x... --gas-bump 30%  

# Update Config During Runtime  
agentgpt session update 0x... \  
    --param mint_rate=70/min \  
    --param gas.max_per_tx=0.075ETH  
```

***

#### **Security Model**

**Session Isolation**

```mermaid
flowchart LR  
    SessionA -->|ZK-Proof| Vault[Secure Enclave]  
    SessionB -->|ZK-Proof| Vault  
    Vault -->|Enrypted| Blockchain  
    style Vault fill:#f9f,stroke:#333  
```

**Protection Layers**:

1. **Hardware Enclaves**: Intel SGX for cross-session data
2. **Tokenized Permissions**: ERC-7645 Session Tokens granular access
3. **Auto-Rollover Keys**: Bi-hourly key rotation via MPC

***

#### **Error Recovery**

**Common Scenarios**:

| Error Code | Resolution Protocol                                    |
| ---------- | ------------------------------------------------------ |
| SESS-511   | `agentgpt session rollback 0x... --block 19438`        |
| SESS-602   | Redeploy session with same ID using `--force-recreate` |
| SESS-743   | Session state recovery via zk-SNARK historical proof   |

**Allowable Downtime**:

```yaml
sla:  
  ethereum: "99.95%"  
  polygon: "99.8%"  
  arbitrum: "99.7%"  
  recovery_time_objective: "15m"  
```

***

#### **Use Cases**

**DeFi Fund Management**:

```yaml
sessions:  
  - id: yield_optimizer  
    chains: [1, 137, 42161]  
    strategy:  
      rebalance:  
        trigger: "apy_delta > 1.8%"  
        max_slippage: 0.5%  
  - id: risk_monitor  
    models:  
      risk_model: "gpt-4-var3.5"  
      frequency: "15s"  
```

**NFT Multi-Chain Drop**:

```solidity
function coordinateMintSessions(bytes32 rootId) external {  
    for (uint i=0; i < chains.length; i++) {  
        sessionManager.createChildSession(rootId, SessionConfig({  
            chainId: chains[i],  
            mintLimit: 1000  
        }));  
    }  
}  
```

***

#### **Quick Start**

1. **Install**:

   ```bash
   pip install agentgpt[multisession]  
   ```
2. **Launch**:

   ```bash
   agentgpt session deploy \  
       --config ./sessions.yaml \  
       --fund 10AGPT  
   ```
3. **Monitor**:

   ```bash
   agentgpt dashboard multisession --refresh 10s  
   ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://agent-gpt.gitbook.io/agent-gpt/usage/multiple-sessions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
