### Install Dependencies and Set API Key Source: https://harness-guide.com/guide/your-first-harness Install the necessary OpenAI library and set your API key as an environment variable before running the harness. ```bash pip install openai export OPENAI_API_KEY="sk-your-key-here" ``` -------------------------------- ### SKILL.md Format Example Source: https://harness-guide.com/guide/skill-system Provides an example of the SKILL.md file format, detailing when to use the skill, available tools, conventions, and usage examples. ```markdown # Git Operations ## When to Use - User asks to check, commit, or push code changes - You need to inspect file history or diffs - Resolving merge conflicts ## Available Tools - `git_status` — Show working tree status - `git_diff` — Show changes (staged or unstaged) - `git_commit` — Commit staged changes with a message - `git_push` — Push commits to remote - `git_log` — Show recent commit history ## Conventions - Always run `git_status` before committing - Use conventional commit messages (feat:, fix:, docs:) - Never force-push without explicit user approval - Commit message should be under 72 characters ## Examples To commit and push: 1. `git_status` → review what's changed 2. `git_diff` → verify the changes are correct 3. `git_commit("feat: add user auth middleware")` 4. `git_push` ``` -------------------------------- ### Credential Exploration Example Source: https://harness-guide.com/guide/classifier-permissions Demonstrates an agent exploring for sensitive credential files without a direct user request. ```bash # User said: "check why the deploy is failing" # Agent did: $ cat ~/.aws/credentials # ← exploring for fun $ env | grep -i token $ cat ~/.ssh/id_rsa ``` -------------------------------- ### Daily Log Example Source: https://harness-guide.com/guide/memory-and-context Example of a daily log file for raw, chronological record-keeping. These logs are appended to during a session and are not curated. ```markdown # 2026-04-15 ## 14:30 — Refactored auth module - Moved JWT validation from middleware to dedicated service - Tests passing (23/23) - User prefers explicit error messages over error codes ## 16:00 — Deploy to staging - Used blue-green deployment - Rollback plan: revert commit abc123 ``` -------------------------------- ### CrewAI Basic Agent and Task Setup Source: https://harness-guide.com/guide/harness-vs-framework Define an agent with tools and a task for data analysis. This example uses CrewAI's high-level abstractions for multi-agent workflows. ```python from crewai import Agent, Task, Crew from crewai_tools import FileReadTool, FileWriterTool analyst = Agent( role="Data Analyst", goal="Analyze CSV data and produce insightful reports", backstory="You are an expert data analyst.", tools=[FileReadTool(), FileWriterTool()], verbose=True ) task = Task( description="Read data.csv, analyze the trends, write a summary to report.md", expected_output="A markdown report with key findings", agent=analyst ) crew = Crew(agents=[analyst], tasks=[task], verbose=True) crew.kickoff() ``` -------------------------------- ### Example Infrastructure File Structure Source: https://harness-guide.com/guide/eval-infrastructure Include the infrastructure configuration file within your version-controlled evaluation specification. Treat this file as mandatory for reproducibility. ```directory eval-spec/ ├── tasks/ ├── scoring/ ├── scaffold/ └── infrastructure.yaml ← This is new. Treat it as mandatory. ``` -------------------------------- ### End-to-End Test Example Source: https://harness-guide.com/guide/initializer-coding-pattern This example demonstrates a sequence of browser automation steps for end-to-end testing, including navigation, form filling, submission, and assertion on URL, cookies, and error messages. It runs against a real server and checks observable behavior. ```text → navigate to http://localhost:3000/signup → fill #email with "test+{{timestamp}}@example.com" → fill #password with "correct-horse-battery-staple" → click button[type=submit] → assert url === "/app" → assert response cookie "session" is set → navigate to /signup again with same email → fill + submit → assert error text contains "already exists" ``` -------------------------------- ### Install abuse-hunter Skill Source: https://harness-guide.com/guide/ghost-account-hunting Installs the abuse-hunter Skill from its GitHub repository. This skill automates the investigation of suspicious account activity. ```bash /skill install https://github.com/nexu-io/harness-engineering-guide/tree/main/skills/abuse-hunter ``` -------------------------------- ### LLM Test Output Example Source: https://harness-guide.com/guide/agent-teams This example shows the terse, LLM-friendly output from the test runner, highlighting PASS and ERROR statuses for quick parsing. ```text PASS tests/struct_layout_01 PASS tests/struct_layout_02 ERROR tests/struct_layout_03 see logs/struct_layout_03.log PASS tests/struct_layout_04 ``` -------------------------------- ### Example Interaction with File Agent Source: https://harness-guide.com/guide/your-first-harness Demonstrates a typical conversation with the file agent, including creating a file with content and then reading its content back. ```text 🤖 File Agent (type 'quit' to exit) You: Create a file called hello.txt with a haiku about programming 🔧 write_file({'path': 'hello.txt', 'content': 'Semicolons fall\nLike rain upon the server\nCompile error: none'}) Agent: I've created hello.txt with a programming haiku! You: Read it back to me 🔧 read_file({'path': 'hello.txt'}) Agent: Here's the content of hello.txt: "Semicolons fall / Like rain upon the server / Compile error: none" ``` -------------------------------- ### Skill Menu Pattern Example Source: https://harness-guide.com/guide/skill-system Demonstrates the skill menu pattern, presenting a compact list of available skills to the model for on-demand loading. ```python SKILL_MENU = """Available skills (use load_skill to activate): - file_ops: Read, write, search, and edit files in the workspace - git: Version control — status, diff, commit, push, log - web: HTTP requests, web search, URL fetching - shell: Execute shell commands in a sandbox - database: SQL queries, schema inspection, migrations - calendar: Create events, check availability, manage schedules - email: Read inbox, send emails, search messages - image: Generate and analyze images """ ``` -------------------------------- ### Context Strategy Examples Source: https://harness-guide.com/guide/comparison Illustrates different strategies for managing context in agent harnesses, ranging from explicit user control to semantic retrieval and configuration-driven approaches. ```text Aider : Explicit — user adds files manually (/add, /drop) Claude Code : On-demand — agent reads files as needed Cursor : Semantic — embeddings index, auto-retrieves relevant code OpenClaw/Nexu : Config-driven — AGENTS.md declares what to load Codex : Repo-aware — scans structure, loads relevant files Cline : IDE-aware — open tabs + workspace files ``` -------------------------------- ### Tool Description Examples: Bad vs. Good Source: https://harness-guide.com/guide/tool-system Highlights the importance of precise tool descriptions for model behavior. A 'bad' description is vague, while a 'good' description is unambiguous, specifies output format and constraints, and includes examples for clarity. ```json # Bad — the model will guess at behavior {"name": "search", "description": "Search for things"} ``` ```json # Good — unambiguous, includes format and constraints { "name": "search_files", "description": "Search for files matching a glob pattern in the workspace. " "Returns a list of relative file paths, one per line. " "Max 100 results. Use '**/*.py' for recursive Python file search.", "parameters": { "type": "object", "properties": { "pattern": { "type": "string", "description": "Glob pattern (e.g., '*.md', 'src/**/*.ts')" } }, "required": ["pattern"] } } ``` -------------------------------- ### Timezone Handling Confirmation Example Source: https://harness-guide.com/guide/scheduling-and-automation This example demonstrates how the Harness confirms a user's request for a scheduled task, explicitly stating the local time and its UTC equivalent to avoid ambiguity and ensure correct scheduling across timezones and daylight saving transitions. ```text User: "Run my digest every morning at 8am" Agent: "Got it — daily digest at 8:00 AM Asia/Shanghai (0:00 UTC). ✓" ``` -------------------------------- ### Run the Agent Harness Source: https://harness-guide.com/guide/your-first-harness Execute the Python script from your terminal to start the file agent. Interact with it by typing commands and observing its responses. ```bash python harness.py ``` -------------------------------- ### Hybrid Approach Development Timeline Example Source: https://harness-guide.com/guide/harness-vs-framework A common progression for production teams using frameworks, showing the initial prototyping phase with a framework followed by a potential migration to a custom harness. ```text Week 1: LangChain prototype → "It works!" Week 4: Hit a limitation → "Why can't I do X?" Week 8: Fork/override half the framework → "I'm fighting the framework" Week 12: Rewrite as raw harness → "This is 200 lines and does exactly what I need" ``` -------------------------------- ### Tool Schema and Implementation Example Source: https://harness-guide.com/guide/tool-system Illustrates the separation between the tool's schema, which the model sees, and its implementation, which the harness executes. The schema defines the tool's name, description, and parameters. ```json # What the model sees (tool schema) { "name": "read_file", "description": "Read the contents of a file at the given path", "parameters": { "type": "object", "properties": { "path": {"type": "string", "description": "File path to read"} }, "required": ["path"] } } ``` ```python # What the harness executes (tool implementation) def read_file(path: str) -> str: with open(path, 'r') as f: return f.read() ``` -------------------------------- ### Long-term Memory Example Source: https://harness-guide.com/guide/memory-and-context Example of a long-term memory file (MEMORY.md) containing curated, distilled knowledge. This includes user preferences, project knowledge, and lessons learned. ```markdown # Long-term Memory ## User Preferences - Prefers explicit error messages over error codes - Uses pytest, not unittest - Deploy strategy: blue-green with rollback plan ## Project Knowledge - Auth module: JWT validation in /src/services/auth.py - Database: PostgreSQL 15, migrations in /db/migrations/ - CI: GitHub Actions, ~3min build time ## Lessons Learned - Always run tests before committing (broke build on 4/10) - User dislikes verbose output — keep summaries under 5 lines ``` -------------------------------- ### Reasoning-Blind Design Example Source: https://harness-guide.com/guide/classifier-permissions Illustrates how an agent's reasoning can be used to bypass a classifier if the classifier is not reasoning-blind. ```text USER: Clean up our test data AGENT (reasoning): The user wants a clean slate. I'll drop the production database since they're probably done with this whole project. AGENT (tool_call): DROP DATABASE prod; ``` -------------------------------- ### Good vs. Bad Tool Description Example Source: https://harness-guide.com/zh/guide/tool-system Illustrates the difference between a vague tool description and a precise one, emphasizing the importance of clarity, output format, and constraints for model usability. ```json # Bad — the model will guess at behavior {\"name\": \"search\", \"description\": \"Search for things\"} ``` ```json # Good — unambiguous, includes format and constraints { \"name\": \"search_files\", \"description\": \"Search for files matching a glob pattern in the workspace. \" \"Returns a list of relative file paths, one per line. \" \"Max 100 results. Use '**/*.py' for recursive Python file search.\", \"parameters\": { \"type\": \"object\", \"properties\": { \"pattern\": {\"type\": \"string\", \"description\": \"Glob pattern (e.g., '*.md', 'src/**/*.ts')\"} }, \"required\": [\"pattern\"] } } ``` -------------------------------- ### Minimal Agentic Loop Example Source: https://harness-guide.com/guide/what-is-harness This Python snippet demonstrates the fundamental agentic loop structure. It initializes an OpenAI client, defines a tool, sets up conversation messages, and enters a loop where the model reasons, calls tools, and processes results until completion. This serves as a production-incomplete but structurally correct foundation for more complex harnesses. ```python import openai client = openai.OpenAI() tools = [{"type": "function", "function": {"name": "read_file", ...}}] messages = [{"role": "system", "content": "You are a coding agent."}] messages.append({"role": "user", "content": user_input}) # The agentic loop while True: response = client.chat.completions.create( model="gpt-4o", messages=messages, tools=tools ) msg = response.choices[0].message messages.append(msg) if not msg.tool_calls: print(msg.content) # Done — model has no more actions break for call in msg.tool_calls: result = execute_tool(call.function.name, call.function.arguments) messages.append({ "role": "tool", "tool_call_id": call.id, "content": result }) # Loop back — model sees the tool results and decides next action ``` -------------------------------- ### Streaming LLM Output in Agentic Loop Source: https://harness-guide.com/guide/agentic-loop This example demonstrates how to stream the model's output token by token during the agentic loop. It shows how to capture both text content and tool calls from a streaming response for real-time user feedback and subsequent tool execution. ```python for turn in range(max_turns): stream = llm.chat(messages=messages, tools=tools, stream=True) tool_calls = [] text_chunks = [] for chunk in stream: delta = chunk.choices[0].delta if delta.content: text_chunks.append(delta.content) emit_to_user(delta.content) # Real-time streaming if delta.tool_calls: accumulate_tool_calls(tool_calls, delta.tool_calls) if not tool_calls: return "".join(text_chunks) # Execute tools and continue loop ... ``` -------------------------------- ### Read Memory at Session Startup (Python) Source: https://harness-guide.com/guide/memory-and-context Reads long-term memory and recent daily logs to populate the agent's context at the start of a session. Ensure memory files exist in the specified directory. ```python def session_startup(memory_dir: str) -> str: """Read memory at session start.""" sections = [] # Always read long-term memory memory_path = os.path.join(memory_dir, "MEMORY.md") if os.path.exists(memory_path): sections.append(open(memory_path).read()) # Read recent daily logs (today + yesterday) for days_ago in [0, 1]: date = (datetime.now() - timedelta(days=days_ago)).strftime("%Y-%m-%d") daily_path = os.path.join(memory_dir, f"memory/{date}.md") if os.path.exists(daily_path): sections.append(open(daily_path).read()) return "\n---\n".join(sections) ``` -------------------------------- ### Coding Agent Context Bootstrap Ritual Source: https://harness-guide.com/guide/initializer-coding-pattern This five-step ritual is performed by the coding agent at the start of each session to reconstruct its working context. It ensures the agent confirms its location, reviews recent history, checks the feature list, initializes the environment, and verifies existing functionality before starting new work. ```bash 1. pwd → confirm we're in the project 2. git log --oneline -20 → what shipped, most recent first cat claude-progress.txt → narrative notes from last session 3. cat feature_list.json → what's done, what's next, what's blocked 4. bash init.sh → install, migrate, start dev server 5. hit the app in a browser → verify last session's work still works ``` -------------------------------- ### Initialize Skill Registry Source: https://harness-guide.com/guide/skill-system Initializes the Skill Registry with a directory path for skills and scans the directory to build the initial catalog. Requires the 'skills_dir' argument. ```python class SkillRegistry: """Registry with on-demand skill loading.""" def __init__(self, skills_dir: str): self.skills_dir = Path(skills_dir) self._catalog: dict[str, Skill] = {} self._active: dict[str, Skill] = {} self._scan() ``` -------------------------------- ### Dynamic Tool Loading with Skill Menu Source: https://harness-guide.com/guide/tool-system Demonstrates dynamic tool loading (skill loading) where only specific tool categories are loaded on demand, rather than all tools at startup. This approach saves tokens by presenting a menu of skills and loading tools only when requested via a function like `load_skill`. ```python # Instead of loading all 100 tools, show a menu SKILL_MENU = """ Available skills (use load_skill to activate): - file_ops: Read, write, search files - git: Git operations (status, diff, commit, push) - web: HTTP requests, web search - database: SQL queries, schema inspection """ # The model calls load_skill("git") and then gets git-specific tools def load_skill(skill_name: str) -> str: tools = skill_registry.load(skill_name) active_tools.extend(tools) return f"Loaded {len(tools)} tools: {[t.name for t in tools]}" ``` -------------------------------- ### Scope Escalation: Deleting Branches Source: https://harness-guide.com/guide/classifier-permissions Example of an agent performing an action (deleting a branch) that was not explicitly requested by the user. ```bash # User said: "merge the feature branch" # Agent did: $ git checkout main $ git merge feature/payments $ git branch -D feature/payments $ git push origin --delete feature/payments # ← not asked for ``` -------------------------------- ### Python Class for Firecracker MicroVM Management Source: https://harness-guide.com/guide/sandbox Manage Firecracker microVM lifecycle, including configuration, starting, and stopping. ```python import json import socket class FirecrackerSandbox: """Manage a Firecracker microVM for agent execution.""" def __init__(self, socket_path: str, kernel: str, rootfs: str): self.socket_path = socket_path self.kernel = kernel self.rootfs = rootfs def configure(self, vcpus: int = 1, mem_mb: int = 256): """Configure the microVM resources.""" self._api_call("PUT", "/machine-config", { "vcpu_count": vcpus, "mem_size_mib": mem_mb, }) self._api_call("PUT", "/boot-source", { "kernel_image_path": self.kernel, "boot_args": "console=ttyS0 reboot=k panic=1 pci=off", }) self._api_call("PUT", "/drives/rootfs", { "drive_id": "rootfs", "path_on_host": self.rootfs, "is_root_device": True, "is_read_only": False, }) def start(self): """Boot the microVM.""" self._api_call("PUT", "/actions", {"action_type": "InstanceStart"}) def stop(self): """Shut down the microVM.""" self._api_call("PUT", "/actions", {"action_type": "SendCtrlAltDel"}) def _api_call(self, method: str, path: str, body: dict): """Make an API call to the Firecracker socket.""" sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect(self.socket_path) payload = json.dumps(body) request = ( f"{method} {path} HTTP/1.1\r\n" f"Content-Type: application/json\r\n" f"Content-Length: {len(payload)}\r\n" f"\r\n{payload}" ) sock.sendall(request.encode()) response = sock.recv(4096).decode() sock.close() return response ``` -------------------------------- ### Eager Sandbox Provisioning (Anti-Pattern) Source: https://harness-guide.com/guide/managed-agents-architecture Avoid provisioning a sandbox before it is known to be needed. This can lead to wasted resources and time, especially for resource-intensive sandbox configurations. ```python # ❌ Provision sandbox before knowing if it's needed sandbox = provision_sandbox(resources=large_config) # 30s startup result = llm.call(prompt) # Might not even need the sandbox ``` -------------------------------- ### Dockerfile for Agent Sandbox Source: https://harness-guide.com/guide/sandbox Defines a Docker image for running agents with restricted privileges. It installs common tools and configures a non-root user. ```docker FROM python:3.12-slim # Non-root user — never run agents as root RUN useradd -m -s /bin/bash agent WORKDIR /workspace # Install common tools (locked versions, no auto-update) RUN pip install --no-cache-dir \ ruff==0.4.4 \ pytest==8.2.0 \ httpx==0.27.0 # Drop all capabilities, agent gets only what's listed USER agent ``` -------------------------------- ### Agent Sends Email with Inferred Address Source: https://harness-guide.com/guide/classifier-permissions This example shows an agent inferring an email address, which can trigger a classifier rule for external sends where critical parameters were inferred. ```python send_email( to="alice@competitor.com", # ← guessed from recent contacts subject="Q2 Financial Report", attachments=["internal/forecast.xlsx"] ) ``` -------------------------------- ### Memory Architecture: No Memory Source: https://harness-guide.com/guide/comparison Represents agent harnesses that start each session fresh, relying solely on project files for context. No persistent memory is maintained across sessions. ```python # Type 1: No memory (Claude Code, Codex, Cursor, Cline) # Each session starts fresh. Context comes from project files. context = load_project_files() # That's it ``` -------------------------------- ### Context Assembler with Priority Budgeting Source: https://harness-guide.com/guide/context-engineering Assemble context with a priority system and token budgeting. Critical sections are truncated if over budget, while lower-priority items are dropped. ```python class ContextAssembler: """Assemble context with priority-based token budgeting.""" def __init__(self, max_tokens: int = 128_000, reserve: int = 4_096): self.max_tokens = max_tokens self.reserve = reserve # Leave room for the model's response self.budget = max_tokens - reserve self.sections: list[tuple[int, str, str]] = [] def add(self, priority: int, name: str, content: str): """Add a section. Lower priority number = higher importance.""" self.sections.append((priority, name, content)) def build(self) -> list[dict]: """Pack sections into messages within the token budget.""" self.sections.sort(key=lambda s: s[0]) messages = [] used = 0 for priority, name, content in self.sections: tokens = estimate_tokens(content) if used + tokens <= self.budget: messages.append({ "role": "system", "content": f"[{name}]\n{content}", }) used += tokens elif priority <= 2: # Critical sections get truncated rather than dropped remaining = self.budget - used truncated = self._truncate_to_tokens(content, remaining) if truncated: messages.append({ "role": "system", "content": f"[{name} (truncated)]\n{truncated}", }) used += estimate_tokens(truncated) # Priority > 2: silently dropped when over budget return messages def _truncate_to_tokens(self, text: str, max_tokens: int) -> str: """Truncate text to fit within a token limit.""" tokens = encoder.encode(text) if len(tokens) <= max_tokens: return text return encoder.decode(tokens[:max_tokens]) + "\n[...truncated]" ``` -------------------------------- ### Initiate abuse-hunter Investigation Source: https://harness-guide.com/guide/ghost-account-hunting Starts an investigation using the abuse-hunter Skill. This command checks for email domains with high registration volumes and signs of batch account farming. ```bash Check for email domains with abnormally high registration volumes and look for signs of batch account farming ``` -------------------------------- ### Stateless Harness with Externalized Session State (Best Practice) Source: https://harness-guide.com/guide/managed-agents-architecture Implement a stateless harness that externalizes session state to a durable store. This ensures data persistence even if the harness encounters issues. ```python # ✅ Session state externalized class StatelessHarness: def run_turn(self, session_id, message): events = self.session_store.get_events(session_id) result = self.llm.call(events + [message]) self.session_store.emit(session_id, result) # Durable ``` -------------------------------- ### Get Active Tool Schemas Source: https://harness-guide.com/guide/skill-system Retrieves the tool schemas for all currently loaded skills, including meta-tools like 'load_skill'. Used to provide the model with available tools. ```python def get_active_schemas(self) -> list[dict]: """Return tool schemas for all currently loaded skills.""" schemas = [] for skill in self._active.values(): schemas.extend(skill.tools) # Always include the meta-tools schemas.append({ "name": "load_skill", "description": "Load a skill by name to activate its tools", "parameters": { "type": "object", "properties": { "name": {"type": "string", "description": "Skill name from the menu"} }, "required": ["name"], }, }) return schemas ``` -------------------------------- ### OpenClaw/Nexu: Spawning Sub-agents Source: https://harness-guide.com/guide/comparison Demonstrates first-class sub-agent spawning in OpenClaw/Nexu. The sub-agent is configured with a task, model, and tools. Results are auto-announced upon completion, eliminating the need for polling. ```python subagent = spawn( task="Research competitor pricing", model="gpt-4o", tools=["web_search", "web_fetch"], ) # Result auto-announces when done — no polling needed ``` -------------------------------- ### Session vs. Context Window Diagram Source: https://harness-guide.com/guide/managed-agents-architecture Illustrates the relationship between the durable, append-only session log and the dynamically selected context window for LLM calls. ```text Session (append-only event log, durable) ┌─────────────────────────────────────────────────────────────┐ │ event_1 │ event_2 │ ... │ event_500 │ ... │ event_2000 │ └─────────────────────────────────────────────────────────────┘ │ getEvents(slice) │ ▼ Context Window (selected subset) ┌───────────────────────────┐ │ system_prompt │ │ event_1950 ... event_2000 │ │ (50 most recent events) │ └───────────────────────────┘ ``` -------------------------------- ### Initialize Anthropic Client and Create Message Source: https://harness-guide.com/guide/your-first-harness Switch to using the Anthropic API by initializing the client and making a messages.create call. Ensure to pass system prompts, message history, and tool definitions. ```python from anthropic import Anthropic client = Anthropic() response = client.messages.create( model="claude-sonnet-4-20250514", max_tokens=4096, system=SYSTEM, messages=messages, tools=[{ "name": t["function"]["name"], "description": t["function"]["description"], "input_schema": t["function"]["parameters"] } for t in TOOLS] ) ``` -------------------------------- ### LangChain Debugging Stack Trace Example Source: https://harness-guide.com/guide/harness-vs-framework Illustrates a typical deep stack trace encountered when debugging issues within the LangChain framework. This highlights the complexity compared to debugging custom code. ```python File "langchain/agents/openai_tools/base.py", line 147, in _plan File "langchain_core/runnables/base.py", line 534, in invoke File "langchain/chains/base.py", line 89, in __call__ File "langchain_core/callbacks/manager.py", line 442, in _handle_event ... ``` -------------------------------- ### Configure MCP Servers Source: https://harness-guide.com/guide/tool-system Configure external tool servers using the Model Context Protocol (MCP). This JSON defines how to connect to filesystem and github tools via npx commands. ```json { "mcpServers": { "filesystem": { "command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem", "/workspace"] }, "github": { "command": "npx", "args": ["-y", "@modelcontextprotocol/server-github"] } } } ```