Skip to main content

Datagusto Agent Control Layer

Project description

agent-control-layer

agent-control-layer is a library designed to add a policy-based control layer to LLM agent frameworks like LangChain and LangGraph. This allows for more granular control over agent behavior, enabling restrictions on specific tool usage and detection of prompt modifications.

Overview

Have you ever experienced your LLM agent calling a tool, only to receive fewer or unexpected results, forcing you back into endless, fragile prompt tweaking? Or perhaps you've found yourself hard-coding every possible scenario, leading to scattered and unmanageable control logic?

agent-control-layer addresses these common pain points by providing a structured, configurable way to manage agent behavior. Instead of relying on unpredictable prompt engineering or brittle hard-coded rules, this library allows you to define behavior rules in YAML. When a specified tool runs and meets a trigger condition (e.g., len(tool_output) < 3 for a web_search tool), additional instructions are automatically injected into the agent's thought process.

This approach offers significant advantages:

  • Maintainable: All control logic resides in a centralized, easy-to-manage location.
  • Testable: Rules are defined as structured configuration, making them testable like code.
  • Collaborative: Non-technical team members can understand and even modify behavior rules.
  • Debuggable: Provides a clear audit trail of what triggered when, simplifying debugging.

Key Features:

  • Contract-Based Policy Engine: Define per-tool contracts in YAML (.dg_acl/*.yaml) with prioritized rules that describe when and how the agent should adapt its behaviour.
  • Dynamic Instruction Injection: When a rule's trigger condition is met, the library automatically injects additional instructions into the agent's reasoning loop—no manual prompt fiddling required.
  • Safe Condition Evaluation: Trigger conditions are evaluated inside a restricted sandbox to prevent unsafe code execution.
  • Seamless LangGraph Integration: Drop-in helper function build_control_layer_tools exposes control_layer_init and control_layer_post_hook tools for effortless use in LangGraph workflows.

Examples

A fully working sample project that demonstrates agent-control-layer in a real LangGraph agent can be found in examples/langgraph-react-agent. If you prefer to learn by example, we recommend running this project first and exploring its code before integrating the library into your own application.

Installation

agent-control-layer can be installed using uv or pip.

uv add -U agent-control-layer
# or
pip install -U agent-control-layer

Basic Usage

Here's an example of integrating agent-control-layer into an existing LangGraph agent.

Integrating with a LangGraph Agent

To integrate agent-control-layer with your LangGraph agent, open your agent's graph definition file (e.g., src/react_agent/graph.py if you're using a template like langgraph-react-agent). Import build_control_layer_tools from agent_control_layer.langgraph and add its output to your agent's existing tool list.

# Example: src/react_agent/graph.py

from agent_control_layer.langgraph import build_control_layer_tools

# ... existing TOOLS definition ...

TOOLS = TOOLS + build_control_layer_tools(State)

# ... rest of the code ...

The build_control_layer_tools function returns control tools provided by agent-control-layer. These tools are automatically used by the agent to evaluate and enforce policies.

Creating Policy Files

agent-control-layer looks for contract files in a directory named .dg_acl located at your project root:

my-agent-project/
├── .dg_acl/
│   ├── contract_file_1.yaml
│   ├── contract_file_2.yaml
│   ├── ...
│   └── contract_file_N.yaml
└── src/
    └── ...

A single YAML file describes the contract for one tool (identified by tool_name). The file must contain the following top-level keys:

Key Type Description
tool_name str The exact name of the tool to which this contract applies.
description str Human-readable summary of what the contract enforces.
rules list[Rule] Ordered list of rules. The library sorts them by priority (ascending) before evaluation.

Each Rule item supports:

Key Type Required Meaning
name str Unique identifier for the rule.
description str What the rule checks / enforces.
trigger_condition str Python expression evaluated against tool_output. Must return True to fire.
instruction str Additional directive injected into the agent when the rule fires.
priority int Lower numbers are evaluated first.

Below is the full search.yaml from the examples directory. It shows two rules that verify the result count and relevance of a hypothetical search tool.

# .dg_acl/search.yaml

tool_name: "search"
description: "Rules for the search tool"
rules:
  # Rule 1 – result count
  - name: "search_result_count"
    description: "The search tool must return at least 5 results."
    trigger_condition: "len(tool_output['results']) < 5"
    instruction: "Ask the user to approve to use the results for further steps."
    priority: 1

  # Rule 2 – relevance check
  - name: "search_result_relevance"
    description: "The search tool must return results that are relevant to the user's query."
    trigger_condition: "len([r for r in tool_output['results'] if r['score'] >= 0.5]) < 5"
    instruction: "Ask the user to approve to use the results for further steps."
    priority: 2

When the search tool finishes, agent-control-layer evaluates these rules in order of priority. The first rule whose trigger_condition evaluates to True wins, and its instruction is injected into the agent via control_layer_post_hook.

💡 Tip: Because contracts are plain YAML, you can version-control them, write unit tests against them, and allow non-developers to propose changes without touching code.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agent_control_layer-0.1.1.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

agent_control_layer-0.1.1-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file agent_control_layer-0.1.1.tar.gz.

File metadata

  • Download URL: agent_control_layer-0.1.1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for agent_control_layer-0.1.1.tar.gz
Algorithm Hash digest
SHA256 fc65ac16cc888bd3665c6d61da1b8485ae3b6566802e89dfb96c7bf331b2a52b
MD5 e354175c820fe436af234720d344e042
BLAKE2b-256 f227f0815451013cfaa1d6c2671bd589ddcc569999809259b53c8cae7de2836a

See more details on using hashes here.

File details

Details for the file agent_control_layer-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for agent_control_layer-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8d87c8724be9e6813f3285eaed743eac04dcdbd6ed06922a75850c8b40e745af
MD5 75b630d17ed6b212cf39c6f42cab007e
BLAKE2b-256 700dfc155dcd19795771f044561a333e7ab967e4b10bd4f258f96c6e5d6b582b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page