Skip to main content
Optimize your Hopx usage to reduce costs while maintaining performance. Follow these best practices to get the most value from your sandboxes.

Cost Optimization Tips

1. Use Smaller Resources

Start with minimal resources and scale up if needed:
  • Python
  • JavaScript/TypeScript
from hopx_ai import Sandbox

# ✅ Good: Start with minimal resources (from template)
# Choose a template with minimal resources
sandbox = Sandbox.create(template="code-interpreter")

# If you need more resources, choose a different template
# or create a custom template with your desired resources

sandbox.kill()  # Cleanup

2. Delete When Done

Always delete sandboxes after use:
  • Python
  • JavaScript/TypeScript
from hopx_ai import Sandbox

sandbox = Sandbox.create(template="code-interpreter")

try:
    result = sandbox.run_code("print('Hello')")
    # Do your work
finally:
    sandbox.kill()  # Always cleanup

3. Use Pause Instead of Keeping Running

Pause preserves state but costs less than keeping sandboxes running:
  • Python
  • JavaScript/TypeScript
from hopx_ai import Sandbox

sandbox = Sandbox.create(template="code-interpreter")
sandbox.run_code("setup_code()")

# Pause when not in use (cheaper than keeping running)
sandbox.pause()

# Resume later
sandbox.resume()
sandbox.run_code("continue_work()")

sandbox.kill()  # Cleanup

4. Share Sandboxes

Reuse sandboxes for multiple operations instead of creating new ones:
  • Python
  • JavaScript/TypeScript
from hopx_ai import Sandbox

# ✅ Good: Reuse sandbox for multiple operations
sandbox = Sandbox.create(template="code-interpreter")
try:
    sandbox.run_code("task1()")
    sandbox.run_code("task2()")
    sandbox.run_code("task3()")
finally:
    sandbox.kill()  # Always cleanup
# Better than creating 3 separate sandboxes

Resource Optimization

Monitor Resource Usage

Use metrics to understand your actual resource needs:
  • Python
  • JavaScript/TypeScript
from hopx_ai import Sandbox

sandbox = Sandbox.create(template="code-interpreter")

# Run your workload
sandbox.run_code("your_workload()")

# Check actual usage
metrics = sandbox.get_metrics_snapshot()
system = metrics.get('system', {})
cpu = system.get('cpu', {})
memory = system.get('memory', {})

print(f"CPU: {cpu.get('usage_percent', 0)}%")
print(f"Memory: {memory.get('used', 0) / 1024 / 1024:.2f}/{memory.get('total', 0) / 1024 / 1024:.2f} MB")

# Choose templates with appropriate resources based on actual usage
sandbox.kill()  # Cleanup

Right-Size Resources

Match resources to your workload:
For simple scripts and basic operations:
  • Python
  • JavaScript/TypeScript
# Choose a template with minimal resources
sandbox = Sandbox.create(template="code-interpreter")

sandbox.kill()  # Cleanup
For data analysis with Pandas:
  • Python
  • JavaScript/TypeScript
# Choose a template with appropriate resources for data analysis
sandbox = Sandbox.create(template="code-interpreter")

sandbox.kill()  # Cleanup
For machine learning training:
  • Python
  • JavaScript/TypeScript
# Choose a template with high resources for ML training
# or create a custom template with your desired resources
sandbox = Sandbox.create(template="code-interpreter")

sandbox.kill()  # Cleanup

Lifecycle Management

Use Timeout for Automatic Cleanup

Automatically clean up sandboxes using timeout_seconds:
  • Python
  • JavaScript/TypeScript
from hopx_ai import Sandbox

# Automatically kills sandbox after timeout
sandbox = Sandbox.create(template="code-interpreter", timeout_seconds=600)
result = sandbox.run_code("print('Hello')")
# Sandbox is automatically killed after 600 seconds
# (No need to manually kill if timeout is set)

Batch Operations

Group operations together to minimize sandbox creation:
  • Python
  • JavaScript/TypeScript
from hopx_ai import Sandbox

# ✅ Good: Batch operations in one sandbox
sandbox = Sandbox.create(template="code-interpreter")
try:
    # Process multiple files
    for file in files:
        sandbox.files.write(f"/workspace/{file.name}", file.content)
        sandbox.run_code(f"process_file('{file.name}')")
finally:
    sandbox.kill()  # Always cleanup

Cost Comparison

StrategyCost ImpactUse Case
Minimal resourcesLowSimple scripts, testing
Right-sized resourcesMediumProduction workloads
Over-provisionedHigh❌ Avoid unless necessary
Reuse sandboxesLowMultiple operations
Create per operationHigh❌ Avoid
Pause when idleLowIntermittent workloads
Keep runningHigh❌ Avoid for idle sandboxes

Next Steps