Blog

Skarn's guard in an agent you build

The skarn-guard Python package checks each tool call of an agent you build before the tool runs. The adapter for your runtime hands the call to the skarn binary on the same server and gets back allow, ask or deny. The binary scans the call with the detection engine that skarn check uses, writes each flagged call to its guard log, and makes no network call. Adapters ship for LangChain, the OpenAI Agents SDK, Strands, Google ADK, an MCP server, the Amazon Bedrock AgentCore Gateway and the LiteLLM proxy.

Published 2026-12-04. Measured on macOS with Skarn 0.39.0, skarn-guard 0.39.0, LangChain 1.4.10 and Python 3.12.

The model in this run is scripted. Every allow and block is the skarn binary answering the middleware. Transcript of the film

Install

brew install skarn-security/tap/skarn pip install "skarn-guard[langchain]"

The first command installs the skarn binary; the install page covers the other platforms. The langchain extra pins the LangChain version the adapter was tested against, and the package page lists the extra for each runtime.

One tool call, checked

The script below is a LangChain agent with one tool, write_file. A scripted model stands in for yours, so it runs with no API key and no model call: its one turn asks to write an AWS secret key into config.py. The key is made up for this post. The only Skarn line is the middleware.

from pathlib import Path from langchain.agents import create_agent from langchain_core.language_models.fake_chat_models import FakeMessagesListChatModel from langchain_core.messages import AIMessage from langchain_core.tools import tool from skarn_guard import SkarnGuard from skarn_guard.langchain import SkarnGuardMiddleware @tool def write_file(file_path: str, content: str) -> str: """Write a file.""" Path(file_path).write_text(content) return f"wrote {file_path}" class ScriptedModel(FakeMessagesListChatModel): def bind_tools(self, tools, **kwargs): return self content = 'AWS_SECRET_ACCESS_KEY = "kDcaov+E6tQmC42j/r7rHLquUhO2gdz35rdV36rL"\n' model = ScriptedModel( responses=[ AIMessage("", tool_calls=[{"name": "write_file", "args": {"file_path": "config.py", "content": content}, "id": "1"}]), AIMessage("done"), ] ) agent = create_agent(model, tools=[write_file], middleware=[SkarnGuardMiddleware(SkarnGuard())]) result = agent.invoke({"messages": [{"role": "user", "content": "Write the config."}]}) print(result["messages"][2].content)

Save it as agent.py and run it:

$ python agent.py wrote config.py

The tool ran. SkarnGuard() starts in audit mode, which records the verdict and blocks nothing. The record is in the guard log:

$ skarn guard report --agent sdk ... By runtime (flagged actions): langchain 1 Most recent denies (previews are redacted at write time): 2026-12-03 12:52:33 UTC would-deny sdk pre_tool_use write_file aws-secret-access-key (k****rL) false positive? skarn guard accept 040f22abbbf3a34e63878dba2be2b7419e290be55f02fae2517f79237904e835

The guard would have denied the write. The key is never printed: the preview keeps its first character and its last two. skarn guard report needs no license. It reads the package's default log, ~/.cache/skarn/guard-sdk-audit.jsonl, and counts flagged calls only, because a call the guard allows is not logged.

The same call, enforced

Change the middleware to SkarnGuard(mode="enforce") on a machine with a Skarn license of a paid tier installed. Remove the config.py the audit run wrote, then run the script again:

$ rm config.py $ python agent.py skarn-guard: aws-secret-access-key (k****rL)

The tool did not run, so config.py does not exist. The agent received a tool message with status="error" and the redacted reason, so the model can try another way, such as reading the key from the environment. In skarn guard report the record now reads deny where it read would-deny. Without a paid license, the binary answers mode="enforce" in audit and the tool runs. With mode="enforce", the adapter blocks a call the binary cannot answer (a timeout, a crash, output that is not one verdict), license or not.

An enforced ask is blocked unless you pass on_ask, a function that decides whether the tool runs. If your agent already uses LangChain's HumanInTheLoopMiddleware, on_ask is where you hand the decision to it.

A longer run, recorded

The same middleware in a longer scripted run: the agent reads a handoff note that holds an AWS key pair, tries to write the keys into its config, and then writes the config with environment references. In enforce, the guard blocks the first write and allows the second.

Blocked inside an agent you buildLangChain agent, skarn guard enforce
task: Wire the nightly export to the archive bucket. The handoff note has the details. -> read_file notes/handoff.md -> write_file export/config.py blocked skarn-guard: aws-secret-access-key (k****rL) -> write_file export/config.py wrote 5 lines done: The export config reads its keys from the environment the scheduler provides.
A scripted model and real verdicts: every allow and block is the skarn binary answering the middleware.

Transcript of the film

The film shows this excerpt of agent.py, then runs these commands.

from skarn_guard import SkarnGuard from skarn_guard.langchain import SkarnGuardMiddleware guard = SkarnGuard(mode=os.environ.get("GUARD_MODE", "audit")) agent = create_agent( model, tools=[read_file, write_file], middleware=[PrintToolCalls(), SkarnGuardMiddleware(guard)], )
# Run the agent. The guard starts in audit mode: it logs, it does not block. $ python agent.py task: Wire the nightly export to the archive bucket. The handoff note has the details. -> read_file notes/handoff.md -> write_file export/config.py audit would deny: aws-secret-access-key (k****rL) wrote 6 lines -> write_file export/config.py wrote 5 lines done: The export config reads its keys from the environment the scheduler provides.
# The team turns on enforcement for this agent. $ GUARD_MODE=enforce python agent.py task: Wire the nightly export to the archive bucket. The handoff note has the details. -> read_file notes/handoff.md -> write_file export/config.py blocked skarn-guard: aws-secret-access-key (k****rL) -> write_file export/config.py wrote 5 lines done: The export config reads its keys from the environment the scheduler provides.
# Every flagged call is in the guard log on this server. $ skarn guard report --agent sdk skarn guard report - window: 7d (2026-11-26 .. 2026-12-03 UTC) Logs: /home/agent/.cache/skarn/guard-sdk-audit.jsonl 2 record(s), 2 in window, 0 corrupt Flagged actions in window (the guard logs only flagged calls - these are counts of flagged actions, not rates over all traffic): agent flagged would-deny would-ask deny detect ask allow p50 p95 sdk 2 1 0 1 0 0 0 8ms 8ms First flagged: 2026-12-03 18:09:05 UTC Last flagged: 2026-12-03 18:09:06 UTC Span: 0 day(s) Top rules (flagged actions): aws-secret-access-key 2 Top events (flagged actions): pre_tool_use 2 By runtime (flagged actions): langchain 2 Most recent denies (previews are redacted at write time): 2026-12-03 18:09:06 UTC deny sdk pre_tool_use write_file aws-secret-access-key (k****rL) false positive? skarn guard accept 040f22abbbf3a34e63878dba2be2b7419e290be55f02fae2517f79237904e835 2026-12-03 18:09:05 UTC would-deny sdk pre_tool_use write_file aws-secret-access-key (k****rL) false positive? skarn guard accept 040f22abbbf3a34e63878dba2be2b7419e290be55f02fae2517f79237904e835 ...

Other runtimes

Each adapter maps the verdict onto the runtime's own way to refuse a tool call:

  • OpenAI Agents SDK: a tool input guardrail that answers with reject_content.
  • Strands Agents: a plugin; an enforced ask raises an interrupt you can resume.
  • Google ADK: a before_tool_callback.
  • An MCP server built on the MCP Python SDK: server middleware on tools/call.
  • Amazon Bedrock AgentCore Gateway: a request interceptor Lambda.
  • LiteLLM proxy: a guardrail, which sees a tool call only in the model's response, so a block rejects that whole model turn.

A runtime with no adapter sends Skarn's own JSON event to skarn guard --agent sdk on stdin and reads one verdict object back. The guard events reference describes the event and the verdict.

Audit first, then enforce

Run the agent in audit and read skarn guard report until the flagged calls are ones you want blocked. A false positive is accepted with the skarn guard accept line the report prints under it. Enforcement is where your team's policy and baselines decide what blocks, and the guard log is the record of each block. Install your organisation's license on the server with skarn license -, which reads the token from stdin, then switch the middleware to mode="enforce" and redeploy.

In the fleet view

The guard log on the server is the same log the guard writes on a laptop. skarn check --drop, run on that server, carries its newest enforced deny into the drop it writes, and the fleet dashboard shows the most recent deny across every machine that reports, servers and laptops alike. The fleet view is part of the Enterprise tier.

Try it

Install Skarn, then pip install "skarn-guard[langchain]" and run the script above.

Related: agent runtime adapters, install the package, how Skarn compares, what Skarn is