If you're building AI tools or autonomous agents with large language models (LLMs), generating code is only half the job. At some point, that code needs to run - often automatically and at scale. But running LLM-generated code in production environments comes with serious risks around security, reliability, and control. That’s exactly the problem Daytona is built to solve.
In this post, I’ll walk through a minimal proof-of-concept that shows how to safely generate and run Python code using LangChain, OpenAI, and the Daytona SDK. The whole workflow happens inside a secure, isolated sandbox environment, a major step forward in making AI-assisted development safer and more reproducible.
Why Daytona?
LLMs are powerful but unpredictable. If you're using them to write code, you can't always guarantee what they'll return. That unpredictability becomes a risk when the code runs in production or on shared infrastructure.
Daytona solves this by providing isolated, programmatically controlled sandbox environments that can be easily created and destroyed. These sandboxes are stateful and can be long-running, making them ideal for agents with tasks that require maintaining state over time. For example, you can spin up a sandbox, write code into it, run the code, read results, and when you're done, tear everything down—without leaving traces or risking the host system.
What This Demo Covers
This proof-of-concept covers:
Creating a Daytona sandbox.
Using LangChain to generate Python code from a prompt.
Executing that code securely in the sandbox.
Performing basic file operations inside the sandbox (write, read, delete).
Cleaning up the sandbox afterward.
Prerequisites
To run the demo, you'll need:
For complete setup instructions, see the Daytona Configuration Guide .
Set your keys with a .env file:
Create a requirements.txt file:
Then set up your environment:
Core Workflow
Here’s the core idea:
1. Generate a prompt describing the feature to implement
2. Generate the code with LangChain + OpenAI
3. Create the sandbox
Learn more: Sandbox Management | Resource Configuration
4. Execute the code
For comprehensive patterns: Process and Code Execution
5. Use the sandbox filesystem
6. Delete the sandbox
Sample console output:
Extending the Pattern: TDD with AI
In the second example, we add a layer of quality control: tests.
1. Prompt for a matching PyTest test suite
2. Generate the test with LangChain + OpenAI
3. Upload both to the sandbox
4. Install and run PyTest inside the sandbox
Sample console output:
5. Self-Healing Code Generation
When tests fail (like in the output above where 2 tests failed), we can automatically regenerate the code with feedback from the test results. By setting a maximum number of retry attempts, we create a self-healing loop where the AI learns from its mistakes and iteratively improves the code until all tests pass:
For the previous failing case, this mechanism successfully generates passing code at the 4th attempt:
Why This Pattern Works
Safe by design: The sandbox ensures no AI-generated code can affect the real environment.
Testable: Adding TDD lets you validate AI output automatically.
Flexible: You can prompt for new functions, generate edge case tests, and reuse the pattern in pipelines.
Next Steps
Customize prompts for your domain (e.g. data parsing, calculations, config generation).
Explore Daytona sandbox options (CPU/memory limits, timeouts).
Extend to multi-file modules or async code.
Final Thoughts
At Devōt, we often build tools that involve dynamic code execution, whether for internal platforms or client-facing products. Daytona gave us a way to experiment safely, with clear boundaries and control.
To be honest, for teams like ours, working at the intersection of AI and engineering, that kind of isolation isn’t just helpful, it’s necessary.
Ready to build your own AI coding assistant? Check out the complete documentation.
