How to Build Ai Tutorial Codes Included: The Production-Grade Blueprint
A deep dive into building the execution engine for AI Tutorial Codes Included. Full tech stack breakdown, secure sandboxing implementation, and scaling strategies for enterprise use cases.
How to Build Ai Tutorial Codes Included: The Production-Grade Blueprint
The GitHub repository “AI Tutorial Codes Included” (by Marktechpost) has become the de-facto standard for learning Agentic AI, RAG, and MCP patterns. But there is a massive gap between a Jupyter Notebook running locally and a production system that serves these agents to 10,000 users.
Building “Ai Tutorial Codes Included” for production doesn’t mean just copying the repo; it means building a Secure Execution Engine that can operationalize these tutorials. You cannot deploy .ipynb files to enterprise infrastructure. You need a stateless, sandboxed, and observable architecture.
This guide details how to build the platform that powers these AI tutorials-transforming static code into a live, secure, and scalable agent runner.
🏗️ The Architecture
We are engineering a Remote Code Execution (RCE) Platform specifically for AI Agents. Standard Docker containers are too slow (cold starts) and insecure for running arbitrary tutorial code. We need microVMs.
- The API Layer (FastAPI): Accepts the user’s prompt or the specific “Tutorial Agent” they want to run (e.g., the “SAGE” agent or “Bioinformatics” agent from the repo).
- The Orchestrator (PydanticAI): Manages the agent’s reasoning loop, structured output, and tool calling.
- The Secure Sandbox (E2B): A Firecracker microVM that executes the generated Python code safely, isolated from your main infrastructure.
- The Data Layer (Redis): Caches agent states and execution logs to prevent redundant computation.
🛠️ The Stack
- Core Framework: PydanticAI (The 2026 standard for type-safe agents, replacing legacy LangChain chains).
- Runtime Security: E2B (Sandboxed Cloud Code Interpreter).
- API Interface: FastAPI (Async, high-performance).
- Observability: Arize Phoenix (Trace the agent’s thought process).
💻 Implementation
The following code implements a production-ready Agent Runner. It takes a complex data analysis request (typical of the “AI Tutorial Codes Included” repo), writes the code, executes it in a remote sandbox, and returns the result-all while handling errors and security boundaries.
We use PydanticAI for the agent logic because it enforces strict typing, which is non-negotiable in production.
import asyncio
import os
from typing import Optional, List
from datetime import datetime
# 2026 Production Standard: PydanticAI + E2B
from pydantic import BaseModel, Field
from pydantic_ai import Agent, RunContext
from e2b_code_interpreter import Sandbox
from fastapi import FastAPI, HTTPException, BackgroundTasks
# Initialize FastAPI
app = FastAPI(title="AI Tutorial Execution Engine")
# --- DATA MODELS (Strict Typing) ---
class ExecutionRequest(BaseModel):
user_id: str
prompt: str
dataset_url: Optional[str] = None
class ExecutionResult(BaseModel):
status: str
logs: List[str]
artifacts: List[str] # URLs to generated charts/files
final_answer: str
execution_time_ms: float
# --- THE AGENT DEFINITION ---
# We define a specialized agent that can write and execute Python code
# This mirrors the "Self-Verifying DataOps Agent" from the repo
agent = Agent(
'openai:gpt-4o',
system_prompt=(
"You are a Senior Data Engineer. You have access to a secure Python sandbox. "
"When asked to analyze data, write Python code to do it. "
"ALWAYS print the final result to stdout. "
"If you generate charts, save them as .png files."
),
deps_type=str, # Passing user_id as dependency for logging
)
# --- THE TOOL (Secure Sandbox) ---
@agent.tool
async def execute_python_code(ctx: RunContext[str], code: str) -> str:
"""
Executes Python code in a secure, isolated E2B sandbox.
Returns the stdout/stderr.
"""
print(f"[{ctx.deps}] Spawning Sandbox for execution...")
# PRODUCTION TIP: Set a timeout to prevent infinite loops costing you money
try:
async with Sandbox(timeout=30) as sandbox:
# If a dataset is provided, we would load it here
# execution = await sandbox.run_code(f"import pandas as pd; df = pd.read_csv('{url}')")
execution = await sandbox.run_code(code)
if execution.error:
return f"Runtime Error: {execution.error.name}: {execution.error.value}"
# Capture artifacts (charts)
for artifact in execution.results:
# In a real app, upload these bytes to S3 and return the URL
pass
return f"STDOUT: {execution.logs.stdout}\nSTDERR: {execution.logs.stderr}"
except Exception as e:
return f"System Error: Sandbox failed to initialize. Details: {str(e)}"
# --- THE API ENDPOINT ---
@app.post("/run-tutorial", response_model=ExecutionResult)
async def run_tutorial_agent(request: ExecutionRequest):
start_time = datetime.now()
try:
# Run the agent
# PydanticAI handles the loop: LLM -> Tool -> LLM -> Answer
result = await agent.run(
request.prompt,
deps=request.user_id
)
duration = (datetime.now() - start_time).total_seconds() * 1000
return ExecutionResult(
status="success",
logs=[msg.content for msg in result.new_messages() if hasattr(msg, 'content')],
artifacts=[],
final_answer=result.data,
execution_time_ms=duration
)
except Exception as e:
# PRODUCTION TIP: Log the full traceback to your observability tool (e.g., Sentry/Datadog)
print(f"CRITICAL FAILURE: {str(e)}")
raise HTTPException(status_code=500, detail="Agent execution failed due to internal error.")
# --- HEALTH CHECK (K8s Readiness) ---
@app.get("/health")
def health_check():
return {"status": "healthy", "service": "tutorial-runner-v1"}
# Usage: uvicorn main:app --workers 4
⚠️ Production Pitfalls (The “Senior” Perspective)
When you take the “AI Tutorial Codes Included” notebooks and put them behind an API, you will hit these three walls immediately:
1. The “Infinite Loop” Bill
Problem: A user asks the agent to “optimize this function,” and the agent writes a while True loop. In a notebook, you press “Stop”. In production, this runs until your credit card declines.
Fix: STRICT timeouts at the Sandbox layer (as shown in the code: Sandbox(timeout=30)). Do not rely on the LLM to stop itself.
2. Dependency Hell
Problem: The tutorial code imports pandas, scikit-learn, matplotlib, and yfinance. A standard container image becomes 5GB+, slowing down cold starts.
Fix: Use Pre-warmed Firecracker MicroVMs. Configure E2B or your custom infrastructure to keep a pool of “Data Science” environments hot. Don’t pip install at runtime; use a custom Dockerfile for your sandbox environment.
3. State Management
Problem: Complex agents (like the SAGE framework) need memory. If your API scales to multiple pods, in-memory lists vanish.
Fix: Externalize state immediately. Use Redis or Postgres to store the messages history. PydanticAI supports custom history stores-use them.
🚀 Final Verdict
The “Ai Tutorial Codes Included” repository is excellent for learning, but it is not a production architecture. It is a collection of logic.
To build the platform that runs this logic:
- Ditch the Notebooks: Port the logic to PydanticAI or LangGraph.
- Isolate Execution: Never run agent-generated code on your API server. Use E2B.
- Type Everything: Use Pydantic models for inputs and outputs to prevent hallucinated data structures from crashing your frontend.
If you are building an enterprise agent platform, this architecture is your baseline. Anything less is a security risk.
Recommended Reads
How to Build Dyad: The Production-Grade Blueprint
A deep dive into building Dyad-the Dual-Agent Generator/Verifier architecture. Full tech stack breakdown, implementation code, and scaling strategies for enterprise use cases.
How to Build Hands On Large Language Models: The Production-Grade Blueprint
A deep dive into building Hands On Large Language Models. Full tech stack breakdown, implementation code, and scaling strategies for enterprise use cases.
How to Build Claude Mem: The Production-Grade Blueprint
A deep dive into building Claude Mem. Full tech stack breakdown, implementation code, and scaling strategies for enterprise use cases.