Skip to main content

What You’ll Learn

  • Connecting to a local LLM via the OpenAI client with a base_url override
  • Checking if the local LLM server is reachable before attempting requests
  • Stripping markdown code fences from LLM responses before execution
  • Writing generated code into a Declaw sandbox filesystem
  • Executing the code securely with sbx.commands.run()
  • Graceful demo mode when the LLM server is not available

Prerequisites

  • Declaw instance running and DECLAW_API_KEY / DECLAW_DOMAIN set
  • A running local LLM server (optional — the example runs in demo mode without it)
Start a local LLM server. With Ollama:
Set LOCAL_LLM_URL and LOCAL_LLM_MODEL in .env if your setup differs from the defaults (http://localhost:11434/v1 and llama3.2).

Code Walkthrough

This example is available in Python. TypeScript support coming soon.

1. Check if the local server is reachable

Before making inference requests, probe the /models endpoint that most OpenAI-compatible servers expose:

2. Connect via OpenAI client with base_url override

Any OpenAI-compatible server works — Ollama, vLLM, LM Studio, llama.cpp server, and more. Pass api_key="not-needed" since local servers typically skip authentication:

3. Strip code fences and execute in a sandbox

4. Demo mode (no LLM server needed)

Expected Output