We are going to build a diagnostic agent that separates pattern recognition from explicit logical inference. It first classifies a raw server log using deep learning, then performs step-by-step root-cause analysis using deep reasoning. This saves on-call engineers from digging through dashboards during an outage.
What you'll need
- Python 3.10 or newer
- An Oxlo.ai API key from https://portal.oxlo.ai
- The OpenAI SDK:
pip install openai
Step 1: Create the Oxlo.ai client
We instantiate the client once and reuse it for both phases. Oxlo.ai is fully OpenAI SDK compatible, so this is a drop-in replacement.
from openai import OpenAI
client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")
Step 2: Classify with deep learning
Deep learning excels at pattern recognition from training data. We will use a fast, general-purpose model to read a raw log line and extract structured fields, which is pure statistical pattern matching.
LOG_LINE = "2024-05-21T14:32:11Z ERROR payment-gateway timeout after 30s txn_id=0x4a2f host=pg-03"
def classify_log(raw_log):
response = client.chat.completions.create(
model="deepseek-v3.2",
messages=[
{"role": "system", "content": "You are a log parser. Extract severity, service, and error_type as JSON. No explanation."},
{"role": "user", "content": raw_log},
],
response_format={"type": "json_object"},
)
return response.choices[0].message.content
classification = classify_log(LOG_LINE)
print("Classification:", classification)
Step 3: Define the reasoning system prompt
Deep reasoning requires explicit chain-of-thought. The system prompt forces the model to state its assumptions and verify each one before concluding.
REASONING_SYSTEM_PROMPT = """You are a senior SRE performing root-cause analysis.
Rules:
1. State every assumption explicitly.
2. For each assumption, cite evidence from the log or your knowledge.
3. If evidence is missing, say what log data would confirm or refute the hypothesis.
4. End with a concrete, actionable fix.
Do not skip steps. Think out loud in <think> blocks."""
Step 4: Diagnose with deep reasoning
Now we route the classified log to a reasoning model. Unlike the classification step, this model spends compute on explicit logical inference rather than simple pattern completion.
def diagnose(classified_log, raw_log):
user_message = f"Classified event: {classified_log}\nRaw log: {raw_log}"
response = client.chat.completions.create(
model="kimi-k2.6",
messages=[
{"role": "system", "content": REASONING_SYSTEM_PROMPT},
{"role": "user", "content": user_message},
],
)
return response.choices[0].message.content
diagnosis = diagnose(classification, LOG_LINE)
print("Diagnosis:\n", diagnosis)
Step 5: Wrap the diagnostic agent
We combine both phases into a single callable agent. This makes it easy to drop into a monitoring pipeline or a Slack bot.
def log_analysis_agent(raw_log):
# Phase 1: Deep learning (pattern recognition)
structured = classify_log(raw_log)
# Phase 2: Deep reasoning (logical inference)
report = diagnose(structured, raw_log)
return {"classification": structured, "diagnosis": report}
if __name__ == "__main__":
result = log_analysis_agent(LOG_LINE)
print(result)
Run it
Running the script produces a structured classification followed by a step-by-step diagnosis. Here is representative output from Oxlo.ai.
Classification: {"severity": "ERROR", "service": "payment-gateway", "error_type": "timeout"}
Diagnosis:
<think>
Assumption 1: The timeout is server-side because the log originates from the payment-gateway service.
Evidence: The host field reads pg-03 and the message states "timeout after 30s".
Assumption 2: A 30-second timeout suggests a downstream dependency, likely a database or third-party API.
Evidence: Payment gateways rarely compute for 30s internally; they wait on external auth or settlement.
Assumption 3: The absence of a retry or circuit-breaker log line means neither is configured or triggered.
Evidence: No retry_count or breaker_state fields appear in the log.
</think>
Concrete fix:
1. Check pg-03 to downstream latency between 14:30 and 14:35.
2. If p99 latency spikes correlate, enable circuit-breaker with a 5s threshold.
3. Add retry logic with exponential backoff for txn_id 0x4a2f and reprocess.
Next steps
Try wiring the agent to a live syslog stream and use the request-based pricing on Oxlo.ai to keep costs flat even when logs get verbose. If you need to analyze visual dashboards alongside text, swap in a vision model like Gemma 3 27B to read screenshots as part of the evidence gathering step.
Top comments (0)