Writing Custom Rules: Tuning Agent Behavior via Rules and Directives
Learn how to configure custom rules and directives in Google Antigravity 2.0 to enforce coding conventions, tech stacks, and security boundaries.
Left to its own devices, an AI coding agent will write code based on its general training weights. While functional, it might import libraries you don't use, ignore your team's naming conventions, or skip writing necessary JSDoc comments.
To maintain codebase health, you must establish clear guardrails.
In Google Antigravity 2.0, developers can enforce strict coding guidelines, safety rules, and architecture choices using Custom Project Rules. Here is how to configure them to keep your agent aligned with your codebase standards.
The Rule Resolution Hierarchy
When Antigravity initializes a task, it scans the repository folder structure to resolve its behavioral constraints. The system follows a specific hierarchy:
[1. Global Rules] -> (~/.gemini/antigravity/rules)
↓
[2. Project Rules] -> (./.gemini/rules)
↓
[3. Directory Rules] -> (./src/components/.rules)
By placing specialized .rules files in specific subdirectories, you can enforce different standards for different layers of your application. For example, your backend API layer can forbid UI imports, while your frontend component directory can require strict TypeScript Prop interfaces.
Anatomy of a Perfect Rules File
Antigravity rules are written in plain Markdown, which the agent parser pre-compiles and injects directly into the LLM system prompt.
Here is a standard, production-ready rule configuration (.gemini/rules):
# Antigravity Project Engineering Rules
## 1. Tech Stack & Library Constraints
* ALWAYS use React + TypeScript for the frontend.
* Do NOT install new npm packages unless explicitly authorized by the user.
* Prefer using existing helper utilities in `src/lib` instead of writing custom helper functions.
## 2. Naming Conventions
* Variables and functions MUST use camelCase (e.g. `fetchUserData`).
* Components and classes MUST use PascalCase (e.g. `UserCard`).
* Files and directories MUST use kebab-case (e.g. `user-card.tsx`).
## 3. Security Guidelines
* Never use raw database queries; always route access through the Prisma/TypeORM repository layer.
* Never print sensitive environment keys or write tokens to log files.
* Do not use `dangerouslySetInnerHTML` in frontend components.
## 4. Documentation (Mandatory JSDoc)
* Every new function must include a concise JSDoc comment explaining *why* it is designed, not just *what* it does.
How Rules Alter Agent Execution
When these rules are parsed, the agent's behavior changes dramatically:
- Preventing Package Bloat: If a task requires dates sorting, instead of running
npm install date-fns, the agent scans the rules, searches the codebase for an existing date utility, and imports it. - Auto-Correction on Lints: If the agent generates camelCase for a file name (e.g.,
userCard.tsx), the linter rule triggers a warning, forcing the agent to rename it touser-card.tsxbefore staging the change. - Safety Halts: If the agent attempts to fetch an environment secret to pass into an API tool, the security rule triggers a permission error, pausing execution for developer review.
Conclusion: Tailoring Your Digital Engineer
By documenting your team's code conventions in markdown files, you convert Antigravity from a generic coding assistant into a bespoke team member who understands your architecture, obeys your security requirements, and formats code exactly the way you like it.
In the final Day 7 article, we will wrap up the series by looking at the paradigm shift from autocompletion to fully autonomous software engineering.
Related Content
Articles
Beyond Autocomplete: Transition to Autonomous Software Engineers
Explore the evolution of AI-powered development, comparing autocomplete tools like Cursor with autonomous coding agents like Google Antigravity 2.0.
Read moreTest-Driven AI Coding: Automating the Code-Test-Fix Loop in Sandboxes
Learn how Google Antigravity 2.0 uses closed-loop TDD to automate writing unit tests, running them in sandboxes, and self-correcting buggy code.
Read morePrompt Caching: Reducing Agent Token Cost by 90% in Large Repositories
Discover how Google Antigravity 2.0 leverages Gemini's Context Caching to make codebase analysis highly cost-effective by caching static contexts.
Read moreRelated Tools
Autogpt
Autonomous AI agent that decomposes complex goals into actionable sub-tasks, leveraging tools and internet access for self-directed execution.
View toolCoze
ByteDance's AI agent builder platform supporting custom bot creation, plugin ecosystems, and multi-channel deployment for conversational AI applications.
View toolCrewai
Multi-agent orchestration framework that coordinates role-playing LLM agents through structured task decomposition and inter-agent communication.
View toolRelated Workflows
AI-Powered Code Review Workflow
Use AI tools to automate and improve your code review process
View workflowBuilding with MCP: Server Development Workflow
Step-by-step workflow for creating and deploying MCP servers
View workflowChatGPT Prompt Engineering Workflow
Master prompt engineering techniques to get the best results from ChatGPT
View workflow