What You’ll Learn
- Building an Agno
Toolkit subclass that executes Python in a Declaw sandbox
- Registering toolkit methods with
self.register()
- Creating an Agno
Agent with the toolkit and an OpenAI model
- Demo mode that exercises the sandbox toolkit directly without needing an OpenAI key
Prerequisites
- Declaw instance running and
DECLAW_API_KEY / DECLAW_DOMAIN set
OPENAI_API_KEY (optional — the example runs in demo mode without it)
pip install declaw python-dotenv agno
This example is available in Python. TypeScript support coming soon.
Code Walkthrough
Subclass Toolkit and register each tool method. The docstring becomes the tool description that the LLM sees:
from agno.tools import Toolkit
from declaw import Sandbox
class DecawTools(Toolkit):
def __init__(self) -> None:
super().__init__(name="declaw")
self.register(self.execute_python)
def execute_python(self, code: str) -> str:
"""Execute Python code in a secure Declaw sandbox."""
sbx = Sandbox.create(template="python", timeout=300)
try:
sbx.files.write("/tmp/code.py", code)
result = sbx.commands.run("python3 /tmp/code.py", timeout=30)
return f"{result.stdout}\n{result.stderr}".strip()
finally:
sbx.kill()
from agno.agent import Agent
from agno.models.openai import OpenAIChat
toolkit = DecawTools()
agent = Agent(
model=OpenAIChat(id="gpt-4o-mini"),
tools=[toolkit],
instructions=[
"You are a helpful assistant that can execute Python code in a secure sandbox.",
"Use the execute_python tool to run code.",
],
show_tool_calls=True,
markdown=True,
)
3. Run the agent
prompt = "Write Python code to generate a multiplication table for 7 (1 through 12) and print it."
agent.print_response(prompt)
4. Demo mode (no API key needed)
from declaw import Sandbox
code = """\
print("Multiplication table for 7:")
for i in range(1, 13):
print(f" 7 x {i:2d} = {7 * i:3d}")
"""
sbx = Sandbox.create(template="python", timeout=300)
try:
sbx.files.write("/tmp/code.py", code)
result = sbx.commands.run("python3 /tmp/code.py", timeout=30)
print(result.stdout)
finally:
sbx.kill()
Expected Output
In demo mode:
=======================================================
Agno (Phidata) + Declaw Sandbox Example
=======================================================
No OPENAI_API_KEY found -- running demo mode.
--- Toolkit Definition ---
class DecawTools(Toolkit):
def __init__(self):
super().__init__(name="declaw")
self.register(self.execute_python)
...
--- Running Code Directly in Declaw Sandbox ---
Code:
print("Multiplication table for 7:")
for i in range(1, 13):
print(f" 7 x {i:2d} = {7 * i:3d}")
stdout: Multiplication table for 7:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
7 x 11 = 77
7 x 12 = 84
Sandbox cleaned up.
=======================================================
Done!
=======================================================