DEV Community

shashank ms
shashank ms

Posted on

Building Utilities Tools with LLMs

I built a terminal utility agent that turns natural language into shell commands, pretty-prints JSON, generates regex, and explains code snippets. It runs on Oxlo.ai with one flat request per operation, so pasting a thousand-line log file costs the same as a one-liner. If you want predictable pricing and an OpenAI-compatible drop-in, this is a project you can ship in under an hour.

What you'll need

  • Python 3.10 or newer
  • The OpenAI SDK: pip install openai
  • An Oxlo.ai API key from https://portal.oxlo.ai
  • A terminal

Step 1: Define the system prompt

I lock the agent's behavior into a strict system prompt so it stays concise and safe. It must output raw strings without markdown fences when generating shell commands or regex.

SYSTEM_PROMPT = """You are a developer utility with four modes.
shell: Convert natural language to a safe shell command. Output only the command, no markdown fences, no explanation.
json: Validate and pretty-print JSON. Output only the formatted JSON.
regex: Generate a regex from a description. Output only the regex pattern.
explain: Explain a code snippet concisely in one paragraph.
If a request is dangerous (deletes, overwrites, rm -rf /), refuse and warn."""

Step 2: Initialize the Oxlo.ai client

I instantiate the OpenAI-compatible client pointing at Oxlo.ai. I use llama-3.3-70b because it follows instructions reliably for structured utility work, but you can swap in deepseek-v3.2 if you are experimenting on the free tier.

from openai import OpenAI

client = OpenAI(
    base_url="https://api.oxlo.ai/v1",
    api_key="YOUR_OXLO_API_KEY"
)

Step 3: Build the request helper

All four utilities share the same call pattern, so I wrap it in a helper to keep the code dry. I keep temperature low to reduce creative hallucinations in shell commands and regex.

def ask_utility(user_message: str, model: str = "llama-3.3-70b") -> str:
    response = client.chat.completions.create(
        model=model,
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": user_message},
        ],
        temperature=0.1,
        max_tokens=1024,
    )
    return response.choices[0].message.content.strip()

Step 4: Implement the utilities

Now I add the four functions. Each prefixes the user input with a mode tag so the model knows exactly which hat to wear. I also add a local JSON pre-check so valid JSON skips the round-trip.

import json

def shell_command(description: str) -> str:
    return ask_utility(f"shell: {description}")

def format_json(raw: str) -> str:
    try:
        parsed = json.loads(raw)
        return json.dumps(parsed, indent=2)
    except json.JSONDecodeError:
        return ask_utility(f"json: {raw}")

def generate_regex(description: str) -> str:
    return ask_utility(f"regex: {description}")

def explain_code(code: str) -> str:
    return ask_utility(f"explain:\n{code}")

Step 5: Wire the CLI

I use argparse to accept a mode flag and a raw string. This keeps the interface unix-friendly and easy to alias in your shell config.

import argparse

def main():
    parser = argparse.ArgumentParser(description="LLM-powered dev utility")
    parser.add_argument("mode", choices=["shell", "json", "regex", "explain"])
    parser.add_argument("input", help="The natural language prompt, code, or data")
    args = parser.parse_args()

    if args.mode == "shell":
        print(shell_command(args.input))
    elif args.mode == "json":
        print(format_json(args.input))
    elif args.mode == "regex":
        print(generate_regex(args.input))
    elif args.mode == "explain":
        print(explain_code(args.input))

if __name__ == "__main__":
    main()

Run it

Save the full script as devutil.py, export your key, and try the modes. Here is a shell session that exercises three of the four tools.

$ export OXLO_API_KEY="sk-..."
$ python devutil.py shell "find all python files modified in the last 24 hours"
find . -name "*.py" -mtime -1

$ python devutil.py json '{"name":"Ada","active":true,"roles":["admin","dev"]}'
{
  "name": "Ada",
  "active": true,
  "roles": [
    "admin",
    "dev"
  ]
}

$ python devutil.py regex "a valid US phone number with optional country code"
^(?:\+1\s?)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$

$ python devutil.py explain "def foo(bar): return bar[::-1]"
This function takes an argument bar and returns its reverse using Python's slice notation [::-1], which steps backward through the sequence.

Next steps

Add function calling so the agent can execute shell commands after user confirmation, or package the script with pipx and publish it to your internal developer tooling registry. If you scale this into a long-context log analyzer, Oxlo.ai's per-request pricing keeps costs flat even when you feed the model multiline stack traces or entire config files.

Top comments (0)