Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# BaseAgent - SDK 3.0

High-performance autonomous agent for [Term Challenge](https://term.challenge). **Does NOT use term_sdk** - fully autonomous with litellm.
High-performance autonomous agent for [Term Challenge](https://term.challenge). **Does NOT use term_sdk** - fully autonomous with Chutes API.

## Installation

Expand Down Expand Up @@ -36,7 +36,7 @@ my-agent/
│ │ ├── loop.py # Main loop
│ │ └── compaction.py # Context management (MANDATORY)
│ ├── llm/
│ │ └── client.py # LLM client (litellm)
│ │ └── client.py # LLM client (Chutes API)
│ └── tools/
│ └── ... # Available tools
├── requirements.txt # Dependencies
Expand Down Expand Up @@ -77,13 +77,13 @@ AUTO_COMPACT_THRESHOLD = 0.85

## Features

### LLM Client (litellm)
### LLM Client (Chutes API)

```python
from src.llm.client import LiteLLMClient
from src.llm.client import LLMClient

llm = LiteLLMClient(
model="openrouter/anthropic/claude-opus-4.5",
llm = LLMClient(
model="deepseek/deepseek-chat",
temperature=0.0,
max_tokens=16384,
)
Expand Down Expand Up @@ -129,7 +129,7 @@ See `src/config/defaults.py`:

```python
CONFIG = {
"model": "openrouter/anthropic/claude-opus-4.5",
"model": "deepseek/deepseek-chat",
"max_tokens": 16384,
"max_iterations": 200,
"auto_compact_threshold": 0.85,
Expand All @@ -142,7 +142,7 @@ CONFIG = {

| Variable | Description |
|----------|-------------|
| `OPENROUTER_API_KEY` | OpenRouter API key |
| `CHUTES_API_KEY` | Chutes API key |

## Documentation

Expand All @@ -151,7 +151,7 @@ CONFIG = {
See [rules/](rules/) for comprehensive guides:

- [Architecture Patterns](rules/02-architecture-patterns.md) - **Mandatory project structure**
- [LLM Usage Guide](rules/06-llm-usage-guide.md) - **Using litellm**
- [LLM Usage Guide](rules/06-llm-usage-guide.md) - **Using Chutes API**
- [Best Practices](rules/05-best-practices.md)
- [Error Handling](rules/08-error-handling.md)

Expand Down
67 changes: 37 additions & 30 deletions agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
SuperAgent for Term Challenge - Entry Point (SDK 3.0 Compatible).

This agent accepts --instruction from the validator and runs autonomously.
Uses litellm for LLM calls instead of term_sdk.
Uses Chutes API for LLM calls instead of term_sdk.

Installation:
pip install . # via pyproject.toml
Expand All @@ -16,56 +16,61 @@
from __future__ import annotations

import argparse
import sys
import time
import os
import subprocess
import sys
import time
from pathlib import Path

# Add parent to path for imports
sys.path.insert(0, str(Path(__file__).parent))


# Auto-install dependencies if missing
def ensure_dependencies():
"""Install dependencies if not present."""
try:
import litellm
import httpx
import pydantic
except ImportError:
print("[setup] Installing dependencies...", file=sys.stderr)
agent_dir = Path(__file__).parent
req_file = agent_dir / "requirements.txt"
if req_file.exists():
subprocess.run([sys.executable, "-m", "pip", "install", "-r", str(req_file), "-q"], check=True)
subprocess.run(
[sys.executable, "-m", "pip", "install", "-r", str(req_file), "-q"], check=True
)
else:
subprocess.run([sys.executable, "-m", "pip", "install", str(agent_dir), "-q"], check=True)
subprocess.run(
[sys.executable, "-m", "pip", "install", str(agent_dir), "-q"], check=True
)
print("[setup] Dependencies installed", file=sys.stderr)


ensure_dependencies()

from src.config.defaults import CONFIG
from src.core.loop import run_agent_loop
from src.llm.client import CostLimitExceeded, LLMClient
from src.output.jsonl import ErrorEvent, emit
from src.tools.registry import ToolRegistry
from src.output.jsonl import emit, ErrorEvent
from src.llm.client import LiteLLMClient, CostLimitExceeded


class AgentContext:
"""Minimal context for agent execution (replaces term_sdk.AgentContext)."""

def __init__(self, instruction: str, cwd: str = None):
self.instruction = instruction
self.cwd = cwd or os.getcwd()
self.step = 0
self.is_done = False
self.history = []
self._start_time = time.time()

@property
def elapsed_secs(self) -> float:
return time.time() - self._start_time

def shell(self, cmd: str, timeout: int = 120) -> "ShellResult":
"""Execute a shell command."""
self.step += 1
Expand All @@ -86,20 +91,22 @@ def shell(self, cmd: str, timeout: int = 120) -> "ShellResult":
except Exception as e:
output = f"[ERROR] {e}"
exit_code = -1

shell_result = ShellResult(output=output, exit_code=exit_code)
self.history.append({
"step": self.step,
"command": cmd,
"output": output[:1000],
"exit_code": exit_code,
})
self.history.append(
{
"step": self.step,
"command": cmd,
"output": output[:1000],
"exit_code": exit_code,
}
)
return shell_result

def done(self):
"""Mark task as complete."""
self.is_done = True

def log(self, msg: str):
"""Log a message."""
timestamp = time.strftime("%H:%M:%S")
Expand All @@ -108,13 +115,13 @@ def log(self, msg: str):

class ShellResult:
"""Result from shell command."""

def __init__(self, output: str, exit_code: int):
self.output = output
self.stdout = output
self.stderr = ""
self.exit_code = exit_code

def has(self, text: str) -> bool:
return text in self.output

Expand All @@ -129,29 +136,29 @@ def main():
parser = argparse.ArgumentParser(description="SuperAgent for Term Challenge SDK 3.0")
parser.add_argument("--instruction", required=True, help="Task instruction from validator")
args = parser.parse_args()

_log("=" * 60)
_log("SuperAgent Starting (SDK 3.0 - litellm)")
_log("SuperAgent Starting (SDK 3.0 - Chutes API)")
_log("=" * 60)
_log(f"Model: {CONFIG['model']}")
_log(f"Reasoning effort: {CONFIG.get('reasoning_effort', 'default')}")
_log(f"Instruction: {args.instruction[:200]}...")
_log("-" * 60)

# Initialize components
start_time = time.time()
llm = LiteLLMClient(

llm = LLMClient(
model=CONFIG["model"],
temperature=CONFIG.get("temperature"),
max_tokens=CONFIG.get("max_tokens", 16384),
)

tools = ToolRegistry()
ctx = AgentContext(instruction=args.instruction)

_log("Components initialized")

try:
run_agent_loop(
llm=llm,
Expand Down
15 changes: 7 additions & 8 deletions astuces/08-cost-optimization.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

## Cost Breakdown

For Claude Sonnet via OpenRouter:
Typical LLM pricing (varies by model):

| Token Type | Cost per 1M |
|------------|-------------|
| Input tokens | $3.00 |
| Cached input | $0.30 (90% off) |
| Output tokens | $15.00 |
| Token Type | Typical Cost per 1M |
|------------|---------------------|
| Input tokens | $1.00 - $15.00 |
| Cached input | 10-50% of input |
| Output tokens | $2.00 - $60.00 |

For a typical task:
- 50 turns
- 100k context average
- 500 output tokens per turn

**Without optimization**: 50 × 100k × $3/1M = **$15 per task**
**With 90% caching**: 50 × 100k × $0.30/1M = **$1.50 per task**
Costs vary significantly by model choice. DeepSeek models are typically more cost-effective than Claude or GPT-4.

## Optimization Strategies

Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ dependencies = [
"tomli-w>=1.0",
"rich>=13.0",
"typer>=0.12.0",
"litellm>=1.50.0",
]

[project.optional-dependencies]
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ tomli>=2.0;python_version<'3.11'
tomli-w>=1.0
rich>=13.0
typer>=0.12.0
litellm>=1.50.0
4 changes: 2 additions & 2 deletions rules/02-architecture-patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ my-agent/
│ │ ├── loop.py # Main loop
│ │ └── compaction.py # Context management (MANDATORY)
│ ├── llm/
│ │ └── client.py # LLM client (litellm)
│ │ └── client.py # LLM client (Chutes API)
│ └── tools/
│ └── ... # Tools
├── requirements.txt # Dependencies
Expand Down Expand Up @@ -275,7 +275,7 @@ flowchart TB
### Implementation

```python
# Définition des outils (format OpenAI/litellm)
# Tool definition (OpenAI-compatible format)
TOOLS = [
{
"name": "run_command",
Expand Down
Loading