AI-Assisted Microservices with Claude Code: Cross-Service Context Management

Nicola·
AI-Assisted Microservices with Claude Code: Cross-Service Context Management

AI-Assisted Microservices with Claude Code: Cross-Service Context Management

Microservices are the worst-case scenario for AI coding agents. The entire architecture is designed to distribute code across multiple repositories, services, and deployment boundaries — and AI agents can only see one repo at a time. When Claude Code opens your `order-service`, it has zero visibility into the `inventory-service` that the order service calls, the `user-service` that provides authentication context, or the shared `proto` files that define the contract between them.

This isn't a minor inconvenience. It's a structural mismatch that causes 40-60% of AI-generated microservices code to fail at the integration boundary. The code compiles, the unit tests pass, and the service crashes the moment it tries to communicate with its neighbors. Cross-service context management is the unsolved problem of AI-assisted microservices development.

Here's how to solve it.

The Microservices Context Problem

Monolithic applications have one critical advantage for AI coding agents: everything is in one place. The database models, API routes, business logic, and shared utilities all live in a single repository with a traceable import graph. An AI agent can follow the dependency chain from any file to any other file.

Microservices shatter this advantage deliberately. A typical microservices architecture distributes code across 5-50 separate repositories, each with its own dependency graph, testing infrastructure, and deployment pipeline. The connections between services exist as:

  • HTTP/gRPC contracts — request/response schemas that must match on both sides
  • Event schemas — Kafka, RabbitMQ, or SNS message formats published by one service and consumed by another
  • Shared type definitions — protobuf files, JSON schemas, or shared npm packages that define common data structures
  • Database boundaries — each service owns its data, but services query each other through APIs rather than direct database access

None of these cross-service relationships appear in a single repository's import graph. They're implicit, distributed, and version-sensitive. Change an event schema in the producer without updating the consumer, and you've created a production incident that no amount of single-service testing would catch.

Why AI Agents Struggle with Microservices

Claude Code, Cursor, GitHub Copilot — every AI coding agent shares the same fundamental limitation when working with microservices. They operate within a single repository's context window.

Single-Service Blindness

When you open `payment-service` in Claude Code and ask it to add a new payment method, the agent sees your service's code perfectly. It understands your database models, route handlers, and business logic. What it doesn't see:

  • The `order-service` endpoint that will call this new payment method
  • The event schema that `notification-service` expects when a payment succeeds
  • The shared `PaymentMethod` enum in your proto definitions that must be updated
  • The API gateway routing rules that need a new path registered

The agent generates a complete, well-tested payment method implementation that breaks three other services the moment it's deployed.

Contract Drift

AI agents don't understand API contracts as living agreements between services. When Claude Code modifies a response schema in `user-service`, it has no way to know that `auth-service`, `order-service`, and `notification-service` all parse that response and expect specific fields. The agent treats the response as a local concern, optimizes it for the current service's needs, and creates a cascading failure across the architecture.

Contract drift is the #1 cause of microservices bugs in AI-assisted development. A 2025 survey of platform engineering teams found that 67% of AI-related production incidents in microservices architectures were caused by undetected contract changes.

Missing Shared Types

Most microservices architectures have a shared types package — protobuf definitions, JSON schemas, or a shared npm/pip package that defines common data structures. AI agents rarely include these shared packages in their context, because they live in a separate repository that isn't part of the current workspace.

Without the shared types, Claude Code reinvents them. It creates local type definitions that look correct but diverge from the canonical shared types in subtle ways — an optional field that should be required, a string that should be an enum, a timestamp in ISO format when the shared schema uses Unix milliseconds.

Cross-Service Dependencies You Must Track

Effective microservices development with AI requires explicit tracking of four dependency types that exist outside any single repository.

API Contracts

Every service-to-service HTTP or gRPC call is a contract. Document these contracts in a format that's accessible across repositories:

  • OpenAPI/Swagger specs — machine-readable API definitions that can be shared across services
  • Protobuf definitions — typed contract files that generate client/server code
  • Contract testing — Pact or similar tools that verify both sides of every contract

The critical insight: your AI agent needs access to the contract definition and the consumer's expectations, not just the producer's implementation.

Event Schemas

Asynchronous communication through events creates implicit dependencies that are even harder for AI agents to track than synchronous API calls.

  • Schema registry — Avro, JSON Schema, or protobuf schemas for every event type
  • Producer/consumer mapping — which services publish which events, and which services subscribe
  • Version compatibility — which event versions are still in production and must remain backward-compatible

When Claude Code modifies an event publisher, it must know every consumer of that event. Without that knowledge, it generates backward-incompatible changes that cause silent data loss in consuming services.

Shared Libraries

Internal shared packages create a hidden dependency graph across services:

  • @company/auth-sdk — authentication utilities used by every service
  • @company/common-types — shared TypeScript interfaces or protobuf generated types
  • @company/logging — standardized logging that all services depend on

Updating a shared library without understanding its consumers is the microservices equivalent of modifying a core function in a monolith without checking its callers. AI agents need the dependency graph to make safe changes.

Infrastructure Contracts

Service mesh configurations, API gateway routes, database migration ordering, and deployment dependencies create another layer of cross-service coupling that AI agents never see. These live in infrastructure-as-code repositories that are rarely included in the coding context.

Giving Claude Code Cross-Service Context

The practical approaches to providing cross-service context, ranked from simplest to most effective.

Manual Context Loading

The simplest approach: manually include contract files in your Claude Code session.

Before modifying `order-service`, open the relevant contract files:

  • The OpenAPI spec for every service that `order-service` calls
  • The event schemas for every event that `order-service` publishes or consumes
  • The shared types package that defines common data structures

This works for small architectures (3-5 services) but becomes unmanageable at scale. You'll spend more time loading context than writing code. And you'll inevitably forget a contract, leading to exactly the kind of integration bug you're trying to prevent.

Service Interaction Maps

Create a `SERVICE_MAP.md` in each repository that documents:

```markdown

order-service

Calls

  • user-service: GET /api/users/:id (auth context)
  • inventory-service: POST /api/inventory/reserve (stock reservation)
  • payment-service: POST /api/payments/charge (payment processing)

Publishes

  • order.created (→ notification-service, analytics-service)
  • order.completed (→ inventory-service, billing-service)

Consumes

  • payment.succeeded (from payment-service)
  • payment.failed (from payment-service)

```

Include this file in your Claude Code context for every session. It gives the agent a map of the service's external dependencies, even though it can't see the actual code in those services. This prevents the most egregious cross-service mistakes, though the agent still can't verify contract compatibility.

Contract-First Development

Structure your development workflow so that contracts are updated before implementations:

  1. Update the shared contract (proto, OpenAPI spec, or shared types package)
  2. Generate client/server stubs from the updated contract
  3. Implement against the generated stubs — this is where Claude Code works

By the time Claude Code touches application code, the contract is already defined and the generated types enforce it. The agent can't accidentally drift from the contract because the type system prevents it.

This is the most reliable approach for preventing AI-generated contract bugs. The overhead is defining contracts first, which is a best practice regardless of AI usage.

How vexp Handles Multi-Repo Microservices

A purpose-built context engine changes the equation entirely. vexp indexes multiple repositories simultaneously and builds a cross-repo dependency graph that connects services through their shared contracts.

When you ask Claude Code to modify an order creation endpoint, vexp doesn't just return the route handler and its local dependencies. It returns:

  • The route handler and middleware chain from `order-service`
  • The relevant shared types from your common types package
  • The API contract that `order-service` exposes to its callers
  • The event schemas that the order creation flow publishes
  • The consumer endpoints in other services that depend on this contract

This cross-repo awareness transforms the agent's ability to make safe microservices changes. Instead of single-service blindness, Claude Code operates with architecture-level visibility — understanding not just the code it's modifying, but the downstream impact of every change.

For teams with 10+ microservices, this typically reduces integration-related bugs by 45-55% and cuts the time spent debugging cross-service issues by nearly half. The token cost of loading cross-repo context is offset by eliminating the 3-4 rounds of fix-deploy-debug that typically follow a blind microservices change.

Common Microservices Tasks with AI

Specific task patterns and how to approach them with proper cross-service context.

Adding a New Endpoint

Without cross-service context: Claude Code generates the endpoint, adds unit tests, and declares it done. The API gateway doesn't know about the new route. The calling service uses the wrong request format. The event triggered by the endpoint has no consumers.

With cross-service context: The agent generates the endpoint, updates the OpenAPI spec, includes the gateway routing configuration, and generates the event schema for any new events. It references the calling service's expected request format from the shared contract.

Cross-Service Refactor

Renaming a field, changing an endpoint path, or modifying an event schema affects every service that depends on it.

Without cross-service context: Claude Code makes the change locally and doesn't know that five other services break.

With cross-service context: The agent identifies every consumer of the changed contract, generates a backward-compatible migration strategy (supporting both old and new field names during transition), and produces a checklist of services that need updating.

Shared Type Updates

Adding a field to a shared type definition (protobuf message, TypeScript interface in a shared package) requires:

  1. Updating the shared type definition
  2. Regenerating client code in every consuming service
  3. Updating every service that uses the type to handle the new field
  4. Ensuring backward compatibility for services that haven't updated yet

Without cross-service context: Claude Code updates the shared type and one service. The other services fail to compile or silently ignore the new field.

With cross-service context: The agent updates the shared type, identifies all consuming services, generates the changes needed in each, and flags any backward-compatibility concerns.

Building a Microservices AI Workflow

The workflow that consistently produces safe microservices changes with AI assistance:

  1. Start with the contract. Before touching application code, identify which cross-service contracts are involved. Use your service interaction map or vexp's cross-repo graph.
  2. Load cross-service context. Include the relevant contracts, shared types, and service interaction documentation in your Claude Code session.
  3. Update contracts first. If the change requires a contract modification, make that change first and verify it's backward-compatible.
  4. Implement with contract awareness. Now let Claude Code generate the implementation. With contracts loaded in context, the agent generates code that conforms to the architecture's expectations.
  5. Run contract tests. Before deploying, run Pact tests or equivalent contract verification to confirm that your changes don't break any consumer.
  6. Deploy in dependency order. Shared types first, then producers, then consumers. Never deploy a consumer before the producer it depends on has been updated.

This workflow adds 10-15 minutes of overhead per change but prevents the 2-4 hours typically spent debugging cross-service integration failures. For teams doing 5+ microservices changes per week, the net time savings is substantial.

The bottom line: AI-assisted microservices development works, but only when the agent has visibility beyond the single service it's editing. Whether you achieve that visibility through manual context loading, service interaction maps, contract-first development, or a cross-repo context engine like vexp — the principle is the same. Give the agent architecture-level awareness, or accept architecture-level bugs.

Frequently Asked Questions

Can Claude Code work with multiple repositories simultaneously?
Claude Code operates within a single working directory by default, which means it can only see one repository at a time. However, you can work around this by using monorepo structures, manually loading files from other repos, or using a multi-repo context engine like vexp that indexes multiple repositories and serves cross-repo dependency information through MCP. The key limitation is context window size — loading entire additional repositories is impractical, so you need targeted cross-repo context.
What's the most common AI-generated bug in microservices architectures?
Contract drift — when an AI agent modifies a service's API response, event schema, or shared type without understanding that other services depend on the current format. This accounts for roughly 67% of AI-related production incidents in microservices architectures. The agent generates code that passes all local tests but breaks consuming services at the integration boundary. Contract-first development and cross-service context loading prevent this category of bug.
How do I give Claude Code context about services it can't directly access?
Three approaches in increasing order of effectiveness: (1) Create a SERVICE_MAP.md in each repository documenting all service-to-service calls, published events, and consumed events. Include it in every Claude Code session. (2) Maintain machine-readable contracts (OpenAPI specs, protobuf definitions) in an accessible location and load relevant contracts when working on cross-service features. (3) Use a multi-repo context engine like vexp that indexes all repositories and automatically serves cross-service dependencies.
Is contract-first development necessary for AI-assisted microservices work?
Not strictly necessary, but highly recommended. Contract-first development (defining API specs and event schemas before implementation) prevents the most dangerous category of AI-generated microservices bugs by constraining the agent to an agreed-upon interface. Without contract-first development, you rely on the AI agent to independently discover and respect cross-service contracts, which it cannot reliably do without explicit cross-repo context.
How does vexp's multi-repo support differ from simply opening multiple repos in an IDE?
Opening multiple repos in an IDE gives you visual access to the code but doesn't create dependency connections between them. vexp indexes each repository's dependency graph and then links them through shared contracts — proto files, shared type packages, API specs. When you query for context about an order endpoint, vexp returns not just the local handler code but the shared types it uses, the services that call it, and the events it publishes. This connected graph is what enables safe cross-service changes with AI assistance.

Nicola

Developer and creator of vexp — a context engine for AI coding agents. I build tools that make AI coding assistants faster, cheaper, and actually useful on real codebases.

Related Articles