Skip to main content

langchain-agentcore-codeinterpreter

PyPI - Version PyPI - License PyPI - Downloads

Amazon Bedrock AgentCore Code Interpreter sandbox integration for Deep Agents.

This package provides AgentCoreSandbox — a SandboxBackendProtocol implementation that wraps AgentCore's Code Interpreter, a secure, isolated MicroVM environment for executing code. AgentCoreSandbox.create() starts a session and stops it when you close the sandbox or leave a with block.

Note: For the LangChain BaseTool integration (used with create_react_agent and LangGraph agents), see langchain-aws[tools]. This package is specifically for the Deep Agents BaseSandbox protocol.

Prerequisites

1. AWS credentials configured via one of the following methods:

# Option 1: Long-lived IAM credentials
export AWS_ACCESS_KEY_ID="your-access-key"
export AWS_SECRET_ACCESS_KEY="your-secret-key"
export AWS_REGION="us-west-2"

# Option 2: Temporary credentials (IAM roles, SSO, STS AssumeRole)
export AWS_ACCESS_KEY_ID="your-access-key"
export AWS_SECRET_ACCESS_KEY="your-secret-key"
export AWS_SESSION_TOKEN="your-session-token"
export AWS_REGION="us-west-2"

# Option 3: AWS CLI profile (picks up ~/.aws/credentials + ~/.aws/config)
aws configure
# or for SSO:
aws configure sso
aws sso login --profile your-profile

Any method supported by the boto3 credential chain works, including EC2 instance profiles, ECS task roles, and environment variables.

2. IAM permissions — your credentials must allow bedrock-agentcore:InvokeCodeInterpreter (or the equivalent action for your region). See the AgentCore Code Interpreter docs for the required IAM policy.

3. Region availability — Code Interpreter is available in select AWS regions. us-west-2 is a safe default. Pass the region to AgentCoreSandbox.create(region=...).

Quick Install

pip install langchain-agentcore-codeinterpreter

Usage

Standalone

from langchain_agentcore_codeinterpreter import AgentCoreSandbox

with AgentCoreSandbox.create(region="us-west-2") as backend:
    result = backend.execute("echo hello")
    print(result.output, result.exit_code)  # "hello" 0

Sessions last 15 minutes by default. Pass session_timeout_seconds (up to 28,800, or 8 hours) for longer agent runs, and identifier to use a custom code interpreter with VPC networking or an execution role.

To configure the CodeInterpreter client yourself, pass a started one to the constructor instead. You then own its lifecycle:

from bedrock_agentcore.tools.code_interpreter_client import CodeInterpreter

interpreter = CodeInterpreter(
    region="us-west-2", integration_source="langchain-deepagents"
)
interpreter.start()
try:
    backend = AgentCoreSandbox(interpreter=interpreter)
    ...
finally:
    interpreter.stop()

With Deep Agents

from deepagents import create_deep_agent

from langchain_agentcore_codeinterpreter import AgentCoreSandbox
from langchain_aws import ChatBedrockConverse

model = ChatBedrockConverse(
    model="us.anthropic.claude-sonnet-4-6",
    region_name="us-west-2",
)

with AgentCoreSandbox.create(
    region="us-west-2", session_timeout_seconds=3600
) as backend:
    agent = create_deep_agent(
        model=model,
        backend=backend,
        system_prompt="You are a coding assistant with sandbox access.",
    )
    result = agent.invoke(
        {
            "messages": [
                {"role": "user", "content": "Create and run a hello world script"}
            ]
        }
    )
    print(result["messages"][-1].content)

File operations

# Upload files
backend.upload_files(
    [
        ("data.csv", b"name,value\nalice,42\nbob,17"),
        ("analyze.py", b"import csv\nprint('ready')"),
    ]
)

# Download files
results = backend.download_files(["data.csv"])
for r in results:
    if r.content is not None:
        print(f"{r.path}: {r.content.decode()}")
    else:
        print(f"Failed: {r.path}: {r.error}")

Session behavior

AgentCore sessions cannot be reconnected once stopped. Each session is a fresh, isolated MicroVM, and its files are gone when it ends. Sessions auto-expire after session_timeout_seconds (default 15 minutes, maximum 8 hours); calls after that return a session-expired error.

execute() honors timeout by running the command under GNU timeout inside the sandbox. A command stopped this way returns exit code 124.

AgentCore caps each request and response at 100 MB and gives each session 10 GB of disk. See AgentCore quotas.

Contributing

See the langchain-aws contributing guide.

cd libs/agentcore-codeinterpreter

# Run unit tests (no network, no AWS credentials needed)
make tests

# Run linter
make lint

# Run integration tests (requires AWS credentials)
make integration_tests

Metadata

Release files for langchain-agentcore-codeinterpreter 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-agentcore-codeinterpreter 0.0.7
File Size Uploaded Provenance Actions
langchain_agentcore_codeinterpreter-0.0.7.tar.gz 227.3 kB Without provenance Details for langchain_agentcore_codeinterpreter-0.0.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for langchain-agentcore-codeinterpreter 0.0.7
File Interpreter ABI Platform Actions
langchain_agentcore_codeinterpreter-0.0.7-py3-none-any.whl Python 3 none any Details for langchain_agentcore_codeinterpreter-0.0.7-py3-none-any.whl

Total release size: 244.3 kB

Release files / langchain_agentcore_codeinterpreter-0.0.7.tar.gz

Download URL langchain_agentcore_codeinterpreter-0.0.7.tar.gz
Size 227.3 kB
Tags Source
SHA-256 checksum
How to use checksums
4ab9094c26d2880bf007386d3d1da8c5fc279b0553cec7225ad1f789e6a6bcce
BLAKE2b-256 checksum
How to use checksums
ba0e74c1f9cb87c576bc756c1d72b16ecde5238c4825479a0303d4217b38e3d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / langchain_agentcore_codeinterpreter-0.0.7-py3-none-any.whl

Download URL langchain_agentcore_codeinterpreter-0.0.7-py3-none-any.whl
Size 17.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
361da5f0860425f5bd2280b5d34b9711e257f676192330e6fde9152a96e58b4e
BLAKE2b-256 checksum
How to use checksums
0ff14bc1792efab5aba94e84edb5f681f24205a8e60722eb6e56dcb2b6202dec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Security for langchain-agentcore-codeinterpreter 0.0.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page