Variables, imports, and in-memory state survive across run_code calls.
The model
Each Podflare sandbox runs a long-lived Python process (Python 3.12).
Every run_code(lang="python") you issue sends a block of code to that
process’s REPL. Execution is in a shared globals dict — so setting a
variable in one call makes it visible to the next.
with Sandbox() as s: s.run_code("x = 42; import pandas as pd") s.run_code("print(type(x), pd.__version__)") # both x and pd are in scope
This matches Jupyter kernel / E2B Code Interpreter / Anthropic
code_execution_20260120 semantics. Agents that were built against
those platforms work the same way on Podflare.
What persists
Named values (x = 1, df = pd.read_csv(...))
Imported modules (import pandas as pd)
Open file handles, sockets, threads, subprocesses
Changes to sys.path, environment, CWD
Anything else you’d expect to live in Python process memory
What does not persist
bash run_code calls. Shell history and $X=foo do NOT carry over —
each bash call is a fresh subprocess. This is intentional; shells are
not the right abstraction for long-lived state.
Child processes you start via subprocess.run. They run, exit,
gone. Use subprocess.Popen if you want a long-lived child.
Fork inherits REPL state
This is the reason we built REPL persistence. When you fork(n), each
child starts from the parent’s exact Python process state.
with Sandbox() as parent: parent.run_code(""" import pandas as pd import numpy as np df = pd.read_csv('/data/1GB.csv') """) # 1 GB DataFrame loaded into parent's Python process children = parent.fork(n=5) # Each child has pandas, numpy, and df already in scope. # No re-loading the CSV. No re-importing modules.
Because fork captures the full guest memory (including the Python
process’s heap) and every child shares that memory copy-on-write until
it mutates a page, the DataFrame is effectively page-level CoW
shared across siblings. Memory cost: near-zero per child.
Exception isolation
A raised exception in one run_code call doesn’t poison the REPL:
with Sandbox() as s: r1 = s.run_code("1 / 0") # r1.exit_code == 1, r1.stderr contains the ZeroDivisionError traceback r2 = s.run_code("print('alive')") # r2.exit_code == 0, r2.stdout == "alive\n"
SystemExit is caught too — its code becomes the call’s exit code, not
a hostd crash.
The protocol
The agent and the in-VM Python process communicate over stdin/stdout with
a minimal framed protocol:
# Agent -> Python (stdin):__PODFLARE_BEGIN__<code>__PODFLARE_END__# Python -> Agent (stdout), one JSON per line, streamed:{"t":"out","d":"hello"}{"t":"err","d":"traceback line"}{"t":"exit","c":0}
The agent translates these into the NDJSON Events the SDK expects
(stdout/stderr/exit). See
agent/repl.py
for the runner (compiled into the agent via include_str!).
Serialization
Only one run_code executes at a time per sandbox. Concurrent calls
serialize via a mutex. If you need parallelism, fork(n) — each child
has its own REPL and you can run them in parallel.
{"docsConfig":{"theme":"mint","$schema":"https://mintlify.com/docs.json","name":"Podflare","colors":{"primary":"#EF4444","light":"#FB923C","dark":"#991B1B"},"logo":{"light":"https://podflare.ai/logo/light.png","dark":"https://podflare.ai/logo/dark.png","href":"https://podflare.ai"},"favicon":"https://podflare.ai/favicon.png","navbar":{"links":[{"href":"https://dashboard.podflare.ai","label":"Dashboard"},{"href":"https://podflare.ai/status","label":"Status"}],"primary":{"type":"github","href":"https://github.com/PodFlare-ai"}},"footer":{"socials":{"github":"https://github.com/PodFlare-ai","website":"https://podflare.ai"}},"search":{"prompt":"Search or ask AI..."},"contextual":{"options":["copy","view","chatgpt","claude","perplexity"],"display":"header"},"styling":{"latex":false},"navigation":{"pages":[]}},"deployment":{"deploymentMetadataValue":{"subdomain":"podflare","hasAudioNarration":false,"actualSubdomain":"podflare","customDomains":["docs.podflare.ai"],"gitSource":{"type":"github","owner":"podflare-ai","repo":"docs","deployBranch":"main","contentDirectory":"","isPrivate":true,"mountPath":"","label":"","_id":"6b10e3399ffae5051c4123ef"},"entitlements":{"AI_CHAT":{"status":"DISABLED"}},"buildId":"6ae9555a7c27206474c7a538:success","clientVersion":"0.0.4453","searchFilterCounts":[{"language":"default","tag":"Documentation","count":22},{"language":"default","tag":"Concepts","count":7},{"language":"default","tag":"API Reference","count":6},{"language":"default","tag":"Reference","count":6},{"language":"default","tag":"Examples","count":5},{"language":"default","tag":"Integrations","count":5},{"language":"default","tag":"Performance","count":4},{"language":"default","tag":"SDKs","count":3},{"language":"default","tag":"Get Started","count":2}]},"assistantConfig":{"enableAskAiButton":false,"enableStarterQuestions":false,"enableContextualStarterQuestions":false,"enableAskAiSearchBar":true},"basePath":"","hasChatPermissions":false,"hasWebMcpPermissions":false,"toggles":[{"name":"kill-isr","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-mintlify-slack","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-integrations-settings","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"rbac-expansion","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-pr-grouping","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"custom-prefix","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"analytics-v7","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"user-flows-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"no-publish-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving-live","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"stripTemperatureMiddleware","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"support-platform-pylon-zendesk","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"instant-rollback","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications-server","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-fable","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fixed-ai-action-pricing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automation-resolve-merge-conflict","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"scoped-publishing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"mintql","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"page-level-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"editor-version-history-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"billing-page-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"full-page-search","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workspace-projects","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fork-pr-previews","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"revalidation-jwt","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"hobby-abuse-scan","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"dashboard-inbox","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"turbopuffer-dual-write","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"approve-review-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"editor-metadata","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"turbopuffer-serve","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"live-agents","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"starter-pricing","enabled":true,"variant":{"name":"treatment","enabled":true,"payload":{"type":"string","value":"treatment"},"feature_enabled":true},"impressionData":false},{"name":"llms-txt-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"auth-vite-origin","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false}],"starterQuestions":[],"googleFontsInlined":false},"customCss":[]}
{"page":{"pagePath":"concepts/repl","slug":"/concepts/repl","pageMetadata":{"title":"Persistent REPL","description":"Variables, imports, and in-memory state survive across run_code calls.","href":"/concepts/repl"},"description":{"compiledSource":"\"use strict\";\nconst {jsx: _jsx} = arguments[0];\nconst {useMDXComponents: _provideComponents} = arguments[0];\nfunction _createMdxContent(props) {\n const _components = {\n p: \"p\",\n ..._provideComponents(),\n ...props.components\n };\n return _jsx(_components.p, {\n children: \"Variables, imports, and in-memory state survive across run_code calls.\"\n });\n}\nfunction MDXContent(props = {}) {\n const {wrapper: MDXLayout} = {\n ..._provideComponents(),\n ...props.components\n };\n return MDXLayout ? _jsx(MDXLayout, {\n ...props,\n children: _jsx(_createMdxContent, {\n ...props\n })\n }) : _createMdxContent(props);\n}\nreturn {\n default: MDXContent\n};\n","frontmatter":{},"scope":{}},"pageType":"default","mdx":{"mdxSource":{"compiledSource":"\"use strict\";const{Fragment:_Fragment,jsx:_jsx,jsxs:_jsxs}=arguments[0];const{useMDXComponents:_provideComponents}=arguments[0];function _createMdxContent(props){const _components={a:`a`,code:`code`,li:`li`,p:`p`,pre:`pre`,span:`span`,strong:`strong`,ul:`ul`,..._provideComponents(),...props.components},{CodeBlock,Heading,Tip}=_components;if(!CodeBlock)_missingMdxReference(`CodeBlock`,true);if(!Heading)_missingMdxReference(`Heading`,true);if(!Tip)_missingMdxReference(`Tip`,true);return _jsxs(_Fragment,{children:[_jsx(Heading,{level:`2`,id:`the-model`,children:`The model`}),`\n`,_jsxs(_components.p,{children:[`Each Podflare sandbox runs a `,_jsx(_components.strong,{children:`long-lived Python process`}),` (Python 3.12).\nEvery `,_jsx(_components.code,{children:`run_code(lang=\"python\")`}),` you issue sends a block of code to that\nprocess’s REPL. Execution is in a shared `,_jsx(_components.code,{children:`globals`}),` dict — so setting a\nvariable in one call makes it visible to the next.`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`4`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`4`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`with`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` Sandbox() `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`as`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` s:`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` s.run_code(`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"x = 42; import pandas as pd\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` s.run_code(`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"print(type(x), pd.__version__)\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` # both x and pd are in scope`})}),`\n`]})})}),`\n`,_jsx(Tip,{children:_jsxs(_components.p,{children:[`This matches Jupyter kernel / E2B Code Interpreter / Anthropic\n`,_jsx(_components.code,{children:`code_execution_20260120`}),` semantics. Agents that were built against\nthose platforms work the same way on Podflare.`]})}),`\n`,_jsx(Heading,{level:`2`,id:`what-persists`,children:`What persists`}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:`Named values`}),` (`,_jsx(_components.code,{children:`x = 1`}),`, `,_jsx(_components.code,{children:`df = pd.read_csv(...)`}),`)`]}),`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:`Imported modules`}),` (`,_jsx(_components.code,{children:`import pandas as pd`}),`)`]}),`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:`Open file handles`}),`, sockets, threads, subprocesses`]}),`\n`,_jsx(_components.li,{children:_jsxs(_components.strong,{children:[`Changes to `,_jsx(_components.code,{children:`sys.path`}),`, environment, CWD`]})}),`\n`,_jsx(_components.li,{children:_jsx(_components.strong,{children:`Anything else you’d expect to live in Python process memory`})}),`\n`]}),`\n`,_jsx(Heading,{level:`2`,id:`what-does-not-persist`,children:`What does not persist`}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsxs(_components.li,{children:[_jsxs(_components.strong,{children:[`bash `,_jsx(_components.code,{children:`run_code`}),` calls.`]}),` Shell history and `,_jsx(_components.code,{children:`$X=foo`}),` do NOT carry over —\neach bash call is a fresh subprocess. This is intentional; shells are\nnot the right abstraction for long-lived state.`]}),`\n`,_jsxs(_components.li,{children:[_jsxs(_components.strong,{children:[`Child processes you start via `,_jsx(_components.code,{children:`subprocess.run`}),`.`]}),` They run, exit,\ngone. Use `,_jsx(_components.code,{children:`subprocess.Popen`}),` if you want a long-lived child.`]}),`\n`]}),`\n`,_jsx(Heading,{level:`2`,id:`fork-inherits-repl-state`,children:`Fork inherits REPL state`}),`\n`,_jsxs(_components.p,{children:[`This is the reason we built REPL persistence. When you `,_jsx(_components.code,{children:`fork(n)`}),`, each\nchild starts from the parent’s exact Python process state.`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`11`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`11`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`with`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` Sandbox() `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`as`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` parent:`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` parent.run_code(`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"\"\"`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` import pandas as pd`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` import numpy as np`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` df = pd.read_csv('/data/1GB.csv')`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"\"\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` # 1 GB DataFrame loaded into parent's Python process`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` children `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` parent.fork(`}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:`n`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`5`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` # Each child has pandas, numpy, and df already in scope.`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` # No re-loading the CSV. No re-importing modules.`})}),`\n`]})})}),`\n`,_jsxs(_components.p,{children:[`Because fork captures the full guest memory (including the Python\nprocess’s heap) and every child shares that memory copy-on-write until\nit mutates a page, the DataFrame is effectively `,_jsx(_components.strong,{children:`page-level CoW\nshared`}),` across siblings. Memory cost: near-zero per child.`]}),`\n`,_jsx(Heading,{level:`2`,id:`exception-isolation`,children:`Exception isolation`}),`\n`,_jsxs(_components.p,{children:[`A raised exception in one `,_jsx(_components.code,{children:`run_code`}),` call doesn’t poison the REPL:`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`6`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`6`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`with`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` Sandbox() `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`as`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` s:`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` r1 `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` s.run_code(`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"1 / 0\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` # r1.exit_code == 1, r1.stderr contains the ZeroDivisionError traceback`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` r2 `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` s.run_code(`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"print('alive')\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` # r2.exit_code == 0, r2.stdout == \"alive\\\\n\"`})}),`\n`]})})}),`\n`,_jsxs(_components.p,{children:[_jsx(_components.code,{children:`SystemExit`}),` is caught too — its code becomes the call’s exit code, not\na hostd crash.`]}),`\n`,_jsx(Heading,{level:`2`,id:`the-protocol`,children:`The protocol`}),`\n`,_jsx(_components.p,{children:`The agent and the in-VM Python process communicate over stdin/stdout with\na minimal framed protocol:`}),`\n`,_jsx(CodeBlock,{numberOfLines:`10`,language:`text`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`text`,children:_jsxs(_components.code,{language:`text`,numberOfLines:`10`,children:[_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`# Agent -> Python (stdin):`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`__PODFLARE_BEGIN__`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`\u003ccode>`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`__PODFLARE_END__`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`# Python -> Agent (stdout), one JSON per line, streamed:`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`{\"t\":\"out\",\"d\":\"hello\"}`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`{\"t\":\"err\",\"d\":\"traceback line\"}`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`{\"t\":\"exit\",\"c\":0}`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{})})]})})}),`\n`,_jsxs(_components.p,{children:[`The agent translates these into the NDJSON Events the SDK expects\n(`,_jsx(_components.code,{children:`stdout`}),`/`,_jsx(_components.code,{children:`stderr`}),`/`,_jsx(_components.code,{children:`exit`}),`). See\n`,_jsx(_components.a,{href:`https://github.com/PodFlare-ai/podflare/blob/main/agent/repl.py`,children:_jsx(_components.code,{children:`agent/repl.py`})}),`\nfor the runner (compiled into the agent via `,_jsx(_components.code,{children:`include_str!`}),`).`]}),`\n`,_jsx(Heading,{level:`2`,id:`serialization`,children:`Serialization`}),`\n`,_jsxs(_components.p,{children:[`Only one `,_jsx(_components.code,{children:`run_code`}),` executes at a time per sandbox. Concurrent calls\nserialize via a mutex. If you need parallelism, `,_jsx(_components.code,{children:`fork(n)`}),` — each child\nhas its own REPL and you can run them in parallel.`]})]})}function MDXContent(props={}){const{wrapper:MDXLayout}={..._provideComponents(),...props.components};return MDXLayout?_jsx(MDXLayout,{...props,children:_jsx(_createMdxContent,{...props})}):_createMdxContent(props)}return{default:MDXContent};function _missingMdxReference(id,component){throw new Error(`Expected `+(component?`component`:`object`)+\" `\"+id+\"` to be defined: you likely forgot to import, pass, or provide it.\")}","frontmatter":{},"scope":{"config":{},"pageMetadata":{"title":"Persistent REPL","description":"Variables, imports, and in-memory state survive across run_code calls.","href":"/concepts/repl"}}},"mdxSourceWithNoJs":null},"mdxExtracts":{"tableOfContents":[{"title":"The model","slug":"the-model","depth":2,"children":[]},{"title":"What persists","slug":"what-persists","depth":2,"children":[]},{"title":"What does not persist","slug":"what-does-not-persist","depth":2,"children":[]},{"title":"Fork inherits REPL state","slug":"fork-inherits-repl-state","depth":2,"children":[]},{"title":"Exception isolation","slug":"exception-isolation","depth":2,"children":[]},{"title":"The protocol","slug":"the-protocol","depth":2,"children":[]},{"title":"Serialization","slug":"serialization","depth":2,"children":[]}],"codeExamples":{}},"jsLinks":[],"subdomain":"podflare","siteName":"Podflare","title":"Persistent REPL - Podflare","contextualStarterQuestions":[],"navMaps":{"firstHrefInVersion":{},"firstHrefInLanguage":{}},"lastModified":"2026-04-19T23:35:43.996Z","originalFileLocation":"concepts/repl.mdx"},"headHtml":"\u003cscript data-mint-head>(function(){var d=document.documentElement,p=null;if(!p){try{p=localStorage.getItem(\"isDarkMode\")}catch(e){}if(p==='true')p='dark';else if(p==='false')p='light';if(p!=='light'\u0026\u0026p!=='dark'\u0026\u0026p!=='system')p=\"system\"}var r=p==='system'?(matchMedia(\"(prefers-color-scheme: dark)\").matches?'dark':'light'):p;d.classList.remove(r==='dark'?'light':'dark');d.classList.add(r);d.setAttribute(\"data-theme-preference\",p)})();\u003c/script>\u003cmeta data-mint-head name=\"description\" content=\"Variables, imports, and in-memory state survive across run_code calls.\">\u003cmeta data-mint-head name=\"application-name\" content=\"Podflare\">\u003cmeta data-mint-head name=\"generator\" content=\"Mintlify\">\u003clink data-mint-head rel=\"apple-touch-icon\" type=\"image/png\" sizes=\"180x180\" href=\"https://podflare.ai/favicon.png\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"https://podflare.ai/favicon.png\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"https://podflare.ai/favicon.png\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/png\" href=\"https://podflare.ai/favicon.png\">\u003cmeta data-mint-head property=\"og:title\" content=\"Persistent REPL - Podflare\">\u003cmeta data-mint-head property=\"og:description\" content=\"Variables, imports, and in-memory state survive across run_code calls.\">\u003cmeta data-mint-head property=\"og:site_name\" content=\"Podflare\">\u003cmeta data-mint-head property=\"og:url\" content=\"https://docs.podflare.ai/concepts/repl\">\u003cmeta data-mint-head property=\"og:type\" content=\"website\">\u003cmeta data-mint-head property=\"og:image\" content=\"https://podflare.mintlify.site/_mintlify/api/og/v2?division=Concepts\u0026amp;title=Persistent+REPL\u0026amp;description=Variables%2C+imports%2C+and+in-memory+state+survive+across+run_code+calls.\u0026amp;theme=85c0ead13df7c18a6484ab5d\u0026amp;signature=642675fa8128eab5c2bb43ddef1fe2726405fe7fa5370d04a45ac59368398f37\">\u003cmeta data-mint-head property=\"og:image:width\" content=\"1200\">\u003cmeta data-mint-head property=\"og:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"twitter:card\" content=\"summary_large_image\">\u003cmeta data-mint-head name=\"twitter:title\" content=\"Persistent REPL - Podflare\">\u003cmeta data-mint-head name=\"twitter:description\" content=\"Variables, imports, and in-memory state survive across run_code calls.\">\u003cmeta data-mint-head name=\"twitter:image\" content=\"https://podflare.mintlify.site/_mintlify/api/og/v2?division=Concepts\u0026amp;title=Persistent+REPL\u0026amp;description=Variables%2C+imports%2C+and+in-memory+state+survive+across+run_code+calls.\u0026amp;theme=85c0ead13df7c18a6484ab5d\u0026amp;signature=642675fa8128eab5c2bb43ddef1fe2726405fe7fa5370d04a45ac59368398f37\">\u003cmeta data-mint-head name=\"twitter:image:width\" content=\"1200\">\u003cmeta data-mint-head name=\"twitter:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"msapplication-config\" content=\"https://mintlify.s3.us-west-1.amazonaws.com/podflare/_generated/favicon/browserconfig.xml?v=3\">\u003cmeta data-mint-head name=\"apple-mobile-web-app-title\" content=\"Podflare\">\u003cmeta data-mint-head name=\"msapplication-TileColor\" content=\"#EF4444\">\u003clink data-mint-head rel=\"canonical\" href=\"https://docs.podflare.ai/concepts/repl\">\u003clink data-mint-head rel=\"alternate\" type=\"application/xml\" href=\"/sitemap.xml\">\u003clink data-mint-head rel=\"alternate\" type=\"text/markdown\" href=\"/concepts/repl.md\">\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https://docs.podflare.ai/#organization\",\"name\":\"Podflare\",\"url\":\"https://docs.podflare.ai\",\"logo\":{\"@type\":\"ImageObject\",\"url\":\"https://podflare.ai/logo/light.png\"}},{\"@type\":\"WebSite\",\"@id\":\"https://docs.podflare.ai#website\",\"name\":\"Podflare\",\"url\":\"https://docs.podflare.ai\",\"publisher\":{\"@id\":\"https://docs.podflare.ai/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https://docs.podflare.ai/concepts/repl#webpage\",\"url\":\"https://docs.podflare.ai/concepts/repl\",\"name\":\"Persistent REPL\",\"description\":\"Variables, imports, and in-memory state survive across run_code calls.\",\"dateModified\":\"2026-04-19T23:35:43.996Z\",\"isPartOf\":{\"@id\":\"https://docs.podflare.ai#website\"},\"breadcrumb\":{\"@id\":\"https://docs.podflare.ai/concepts/repl#breadcrumb\"}},{\"@type\":\"BreadcrumbList\",\"@id\":\"https://docs.podflare.ai/concepts/repl#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Concepts\",\"item\":\"https://docs.podflare.ai/concepts/sandboxes\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Persistent REPL\",\"item\":\"https://docs.podflare.ai/concepts/repl\"}]},{\"@type\":[\"Article\",\"TechArticle\"],\"@id\":\"https://docs.podflare.ai/concepts/repl#article\",\"headline\":\"Persistent REPL\",\"name\":\"Persistent REPL\",\"description\":\"Variables, imports, and in-memory state survive across run_code calls.\",\"url\":\"https://docs.podflare.ai/concepts/repl\",\"mainEntityOfPage\":{\"@id\":\"https://docs.podflare.ai/concepts/repl#webpage\"},\"dateModified\":\"2026-04-19T23:35:43.996Z\",\"publisher\":{\"@id\":\"https://docs.podflare.ai/#organization\"},\"isPartOf\":{\"@id\":\"https://docs.podflare.ai#website\"}}]}\u003c/script>\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@type\":\"WebSite\",\"name\":\"Podflare\",\"publisher\":{\"@type\":\"Organization\",\"name\":\"Podflare\"},\"provider\":{\"@type\":\"Organization\",\"name\":\"Mintlify\",\"url\":\"https://mintlify.com\"}}\u003c/script>","agentPointerHref":"/concepts/repl.md","siteName":"Podflare","nav":{"global":null,"tabs":[{"tab":"Documentation","groups":[{"group":"Get Started","pages":[{"title":"Introduction","href":"/introduction"},{"title":"Quickstart","href":"/quickstart"}]},{"group":"Concepts","pages":[{"title":"What is a Podflare sandbox?","href":"/concepts/sandboxes"},{"title":"Fork","href":"/concepts/fork"},{"title":"Warm Pool","description":"How create() returns in 6 ms server-side, 187 ms end-to-end.","href":"/concepts/warm-pool"},{"title":"Persistent REPL","href":"/concepts/repl"},{"title":"Observability","description":"W3C Trace Context propagation, structured spans, JSON logs.","href":"/concepts/observability"},{"title":"Authentication","href":"/concepts/auth"},{"title":"Egress policy","href":"/concepts/egress"}]},{"group":"SDKs","pages":[{"title":"Python SDK","href":"/sdks/python"},{"title":"TypeScript SDK","href":"/sdks/typescript"},{"title":"MCP server","href":"/sdks/mcp"}]},{"group":"Examples","pages":[{"title":"Examples","href":"/examples/overview"},{"title":"Code-interpreter agents","href":"/examples/code-interpreter"},{"title":"Data analysis","href":"/examples/data-analysis"},{"title":"Database work","href":"/examples/database"},{"title":"Web automation","href":"/examples/web-automation"},{"title":"Dev environments","href":"/examples/dev-environments"}]},{"group":"Integrations","pages":[{"title":"OpenAI Agents SDK","href":"/integrations/openai-agents"},{"title":"Vercel AI SDK","href":"/integrations/vercel-ai-sdk"},{"title":"Anthropic Messages API","href":"/integrations/anthropic"},{"title":"Claude Desktop","href":"/integrations/claude-desktop"},{"title":"OpenClaw + ClawHub","href":"/integrations/openclaw"}]}]},{"tab":"API Reference","pages":[{"title":"API Reference","href":"/api-reference/introduction"}]},{"tab":"Performance","pages":[{"title":"Performance","href":"/architecture/performance"}]}]},"basePath":"","title":"Persistent REPL - Podflare","deploymentId":"dpl_BLZw3QngerHodH9E8mGgC4v752y3","buildId":"dpl_BLZw3QngerHodH9E8mGgC4v752y3","dynamics":[],"chunks":[]}