Skip to content

CodeInterpreter

Source: src/AWS/BedrockAgentCore/CodeInterpreter.ts

A custom Amazon Bedrock AgentCore Code Interpreter — an isolated sandbox where agents execute Python/JavaScript/TypeScript code.

A custom interpreter controls the sandbox’s network mode and execution role. All configuration is create-only (the API has no update operation); property changes trigger a replacement.

Sandboxed Interpreter (no network egress)

import * as AgentCore from "alchemy/AWS/BedrockAgentCore";
const interpreter = yield* AgentCore.CodeInterpreter("Sandbox", {});

Interpreter with Public Egress

const interpreter = yield* AgentCore.CodeInterpreter("PublicSandbox", {
networkConfiguration: { networkMode: "PUBLIC" },
});
// init
const startSession = yield* AgentCore.StartCodeInterpreterSession(interpreter);
const invoke = yield* AgentCore.InvokeCodeInterpreter(interpreter);
const stopSession = yield* AgentCore.StopCodeInterpreterSession(interpreter);
return {
fetch: Effect.gen(function* () {
// runtime
const session = yield* startSession({ sessionTimeout: "5 minutes" });
const result = yield* invoke({
sessionId: session.sessionId,
name: "executeCode",
arguments: { language: "python", code: "print(1 + 1)" },
});
const output = yield* Stream.runCollect(result.stream);
yield* stopSession({ sessionId: session.sessionId });
return HttpServerResponse.json({ output: Array.from(output) });
}),
};