I built a deep reasoning security agent that audits Python code for subtle vulnerabilities. It runs a fast first pass, then uses a second reasoning step to challenge its own findings and catch bugs that shallow scans miss. If you ship code or review pull requests, this tool gives you structured, high-confidence security reports without the noise of a standard linter.
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. Oxlo.ai uses request-based pricing, so long code snippets do not blow up your bill when you feed entire modules into the context window.
Step 1: Bootstrap the project
I create a single file called audit.py and initialize the Oxlo.ai client. I pull the API key from the environment and point the base URL to Oxlo.ai.
import os
import json
from openai import OpenAI
client = OpenAI(
base_url="https://api.oxlo.ai/v1",
api_key=os.environ.get("OXLO_API_KEY")
)
Step 2: Write the system prompt
The system prompt is the core of the agent. I force the model to critique its own reasoning before outputting anything, which reduces hallucinated vulnerabilities and forces deeper analysis.
SYSTEM_PROMPT = """You are a deep-reasoning security analyst. Analyze code for vulnerabilities.
Rules:
1. Trace all user inputs through the system.
2. Identify trust boundaries and authentication gaps.
3. Check for injection, path traversal, insecure deserialization, and secrets leakage.
4. Before finalizing, critique your own reasoning. What assumptions did you make? What did you miss?
5. Output valid JSON with keys: summary (string), severity (string), vulnerabilities (list of objects with keys: title, description, line_number, fix), and confidence (string)."""
Step 3: Run a shallow first pass
I start with Llama 3.3 70B on Oxlo.ai because it is fast and cost-effective for initial triage. The goal is to surface obvious issues so the deep reasoning step has something concrete to validate or reject.
def shallow_scan(code: str) -> str:
response = client.chat.completions.create(
model="llama-3.3-70b",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": f"Perform a shallow initial scan:\n
```python\n{code}\n```
"}
],
temperature=0.2,
max_tokens=2000,
)
return response.choices[0].message.content
Step 4: Perform deep reasoning critique
This is where security analysis gets serious. I feed the shallow findings and the original code into Qwen 3 32B, a model with strong reasoning and agentic capabilities. I explicitly ask it to challenge assumptions, look for second-order effects, and strip out false positives. I enable JSON mode so the final report is machine readable.
def deep_reasoning_audit(code: str, shallow_findings: str) -> dict:
deep_prompt = f"""The user submitted this code:
```python
{code}
```
An initial shallow scan produced these findings:
{shallow_findings}
Your task: perform a deep reasoning audit. Challenge the shallow findings. Look for false positives, subtle logic bugs, and second-order effects. Consider race conditions, supply chain issues, and side-channel leaks. Return a final, consolidated JSON report. Do not wrap JSON in markdown fences."""
response = client.chat.completions.create(
model="qwen-3-32b",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": deep_prompt},
],
temperature=0.1,
max_tokens=4000,
response_format={"type": "json_object"},
)
raw = response.choices[0].message.content
return json.loads(raw)
Step 5: Orchestrate the full pipeline
I wire the two passes together in a single audit() function and add a small test harness with intentionally vulnerable code. When you run this, the agent first scans, then reasons, then prints a structured report.
def audit(code: str) -> dict:
print("Running shallow scan...")
shallow = shallow_scan(code)
print("Running deep reasoning audit...")
return deep_reasoning_audit(code, shallow)
if __name__ == "__main__":
test_code = '''
import os
import pickle
def load_user_prefs(user_id):
path = f"/tmp/prefs/{user_id}.dat"
with open(path, "rb") as f:
return pickle.load(f)
def run_command(cmd):
os.system(cmd)
'''
report = audit(test_code)
print(json.dumps(report, indent=2))
Run it
Save the file and run it from your terminal.
python audit.py
You should see output similar to this.
{
"summary": "Deep reasoning audit confirmed 2 critical issues and rejected 1 false positive from the shallow scan.",
"severity": "critical",
"confidence": "high",
"vulnerabilities": [
{
"title": "Arbitrary Code Execution via Pickle Deserialization",
"description": "The function load_user_prefs constructs a path from user_id and passes it directly to pickle.load. An attacker who controls the filesystem can replace the file with a malicious pickle payload, leading to RCE.",
"line_number": 6,
"fix": "Replace pickle with json. If complex objects are required, use a restricted serializer and verify integrity with HMAC."
},
{
"title": "Unsanitized os.system Call",
"description": "run_command passes its argument directly to os.system without validation, allowing shell command injection.",
"line_number": 9,
"fix": "Use subprocess.run with shell=False and pass the command as a sequence of arguments."
}
]
}
Next steps
Wire this agent into your CI pipeline by reading diff files instead of hard-coded strings. You could also swap the first pass to DeepSeek V3.2 to stay on Oxlo.ai's free tier, or upgrade to Kimi K2.6 when you need vision analysis of architecture diagrams alongside the code.
Top comments (0)