Coding agents

Understanding your codebase

View as Markdown

One of the most important jobs of a software engineer is building a mental map of a codebase and deeply understanding how the system works. As a project grows, it gets harder and harder to find the right code.

In the past, going through a large codebase meant memorizing regex patterns or learning specialized tools to search effectively. With coding agents, you can describe what you're looking for in natural language and let the agent use tools to find it for you.

Grok Build gives agents specific tools to search effectively, and understanding how these tools work will help you ask better questions and get more accurate results.

The most precise way to find a piece of code is to look for an exact match, whether that's the name of a function, a variable, or other parts of code.

Agents can use grep, a tool for finding exact strings, and they're able to create more complicated regex patterns or word boundary matches. There have also been improvements over grep, like ripgrep, which make searching recursive.

Both of these tools work great. However, Grok Build has improved grep even further with Instant Grep, which can speed up agentic searches on large codebases by a significant amount over ripgrep.

You don't need to configure anything or change your behavior to use these tools. Grok Build provides them automatically, and when you chat with an agent, it uses them under the hood.

Search by meaning

When you don't know the exact symbol or text, Agent can search your codebase by meaning. It combines this with grep to find relevant files and trace exact references. See how Agent searches your codebase for details.

Asking good questions

The way you phrase questions affects which search tools the agent uses and how effective the results are. It's a spectrum from specific to broad: you might start by asking for an exact text match like the name of a function, all the way up to broad searches like "help me understand what happens when a user submits a payment in our application."

Start specific when you know what you're looking for. Go broad when you're exploring unfamiliar territory. Here are some examples.

In this first example, the agent starts with grep because the prompt asks for something specific. It searches for import.*PaymentService to find every file that references the service.

Ask mode example: Targeted search
Find all files that import from our PaymentService and show me how they handle the PaymentFailedError.
See the agent's response
Grepped import.*PaymentService
Grepped PaymentFailedError
Read src/api/routes/payments.ts
Read src/api/routes/subscriptions.ts
Read src/workers/BillingWorker.ts
Files importing PaymentService (5 total):
FileHow it handles PaymentFailedError
api/routes/payments.tsCatches and returns 402 with error code
api/routes/subscriptions.tsCatches, logs, and retries once before failing
workers/BillingWorker.tsCatches, adds to dead letter queue for manual review
services/OrderService.tsLets it bubble up to the route handler
services/RefundService.tsDoes not catch it (potential bug: refund failures crash silently)
Note: RefundService.ts imports PaymentService but has no error handling for PaymentFailedError. If a refund fails, the worker crashes without logging.

When you're exploring unfamiliar territory, go broader. The next example asks a general question about how the application handles failed payments. Notice how the first tool call is "Search codebase." Agent finds relevant files, then follows up with grep to fill in the details.

Ask mode example: Broad exploration
How does our application handle failed payments? Walk me through the error flow from the checkout form to the error message the user sees.
See the agent's response
Searched codebase payment failure handling
Read src/pages/Checkout.tsx
Read src/services/PaymentService.ts
Read src/api/routes/payments.ts
Grepped PaymentFailedError
Payment failure flow:
1.Frontend (Checkout.tsx:67): User clicks Pay. handleSubmit() calls PaymentService.charge().
2.Service (PaymentService.ts:89): Creates a Stripe PaymentIntent. If Stripe returns an error, it throws PaymentFailedError with the Stripe error code.
3.API route (payments.ts:34): Catches PaymentFailedError and returns a 402 with { error: code, message: userMessage }.
4.Frontend (Checkout.tsx:78): Catches the 402 response and displays error.message in a toast notification.
Edge case: If the Stripe API times out (5s limit in PaymentService.ts:92), it throws a generic ServiceUnavailableError instead of PaymentFailedError. The frontend handles this differently; it shows a retry button instead of an error message.

The explore subagent

The agent can also spawn subagents to complete tasks more efficiently.

There's a built-in Explore subagent that helps you search through your codebase. The Explore subagent runs in its own context window from the parent agent and uses a faster model, so it can execute many parallel searches without bloating the main conversation.

You don't have to invoke this manually. The agent will use it when it decides it's relevant. However, you can also ask to use the subagent directly if you prefer.

As the foundations course, explained, keeping an eye on your context usage matters. If you're searching through many files in your codebase, this will generate a lot of context. Subagents can significantly improve your context management by only returning their findings and keeping the main conversation focused.

Architectural diagrams

For large or unfamiliar codebases, you can ask the agent to generate architectural diagrams, like Mermaid diagrams, to help you visualize your codebase.

Ask mode example: Architecture diagram
Create a Mermaid diagram showing the data flow for our payment system, including the checkout form, API routes, payment service, and Stripe integration.
See the agent's response
Searched codebase payment system architecture
Read src/pages/Checkout.tsx
Read src/services/PaymentService.ts
Read src/api/routes/payments.ts
Grepped stripe.*webhook
Read src/api/webhooks/stripe.ts
Here's the payment system data flow:
Mermaid diagram source
flowchart TB
    A[Checkout Form] --> B[API Route]
    B --> C[PaymentService]
    C --> D[Stripe API]
    D --> E[Webhook Handler]
    E --> F[(Database)]
    F --> G[Order Service]
    G --> H[Email Service]
How it works:
1.Checkout Form (Checkout.tsx) — collects payment details and calls /api/payments
2.API Route (payments.ts) — validates the request, creates an order record, and delegates to PaymentService
3.PaymentService (PaymentService.ts) — creates a Stripe PaymentIntent and returns the client secret
4.Stripe API — processes the charge and sends a webhook on success or failure
5.Webhook Handler (webhooks/stripe.ts) — receives payment_intent.succeeded, updates the order status in the database
6.Order Service — triggers fulfillment and queues a confirmation email
Key detail: The checkout form doesn't wait for payment confirmation. It creates the order as pending, and the webhook handler moves it to completed asynchronously. This means the user sees a confirmation page before Stripe has finished processing.

These diagrams are useful for onboarding, documentation, and design reviews. They can also reveal architectural issues, like a service that depends on too many other services, or a data flow that takes an unexpected path.

Common failure pattern: changing before understanding

A common mistake is asking the agent to change code without first understanding what exists. The agent might create a new utility function when one already exists, or use a different pattern than the rest of your codebase.

Before asking for changes, ask the agent to explore first:

Ask mode example: Explore before changing
Before making any changes, show me how our existing form validation works. What patterns do we use, and where are the shared validators?
See the agent's response
Searched codebase form validation patterns
Grepped validate.*schema
Read src/lib/validators.ts
Read src/components/ui/FormField.tsx
Form validation patterns in this project:
•Schema definitions: Zod schemas in src/lib/validators.ts. Each form has a corresponding schema (e.g., loginSchema, profileSchema, checkoutSchema).
•Validation hook: useFormValidation(schema) in src/hooks/useFormValidation.ts wraps zod with real-time field validation.
•Error display: FormField component in src/components/ui/FormField.tsx accepts an error prop and shows inline error messages below the input.
•Server-side: API routes validate with the same Zod schemas via validateBody() middleware in src/middleware/validation.ts.
Pattern to follow: Define a Zod schema in validators.ts, use useFormValidation in the component, and add validateBody(schema) to the API route.

Coding agents take your requests literally. Where you don't provide intent, they use their best judgment. Sometimes that works well. But for changes that need to follow existing patterns, you'll get better results when you understand the codebase first and know specifically what to ask for.

What's next

You can find and understand code. In the next chapter, you'll learn how to go from understanding to shipping: planning features, writing tests, and turning designs into code.

You've completed this chapter

Next: Developing features

Last updated: November 23, 2026