In a major engineering milestone achieved through a close technical partnership between Meta and Google, the development team behind Instagram Direct has successfully modernized its massive messaging architecture by migrating entirely to Jetpack Compose. Handling billions of user messages daily across hundreds of millions of active accounts, Instagram Direct represents one of the most critical and heavily optimized surfaces within the entire mobile application. For years, the engineering team had squeezed every possible micro-optimization out of the legacy Android View system. However, maintaining and expanding such a heavily optimized legacy surface inevitably generates significant technical debt and engineering overhead, particularly as modern development workflows increasingly embrace declarative user interfaces and AI-assisted coding tools.

Rather than treating the transition as a routine UI modernization project, the Instagram team utilized the migration to build an entirely AI-native UI codebase. The results of this architectural shift are striking: the new implementation is 50 percent smaller than the original code, while achieving a 35 percent reduction in AI agent execution time, 32 percent fewer engineer-agent exchanges, and a 33 percent reduction in token costs. Furthermore, through deep performance optimizations carried out in collaboration with Google, the engineering partnership improved Jetpack Compose not just for the Instagram application, but for the broader Android developer ecosystem at large.

Modernizing the Codebase at Massive Scale

Artificial intelligence has rapidly evolved into a daily companion for software engineers across the technology industry. Applying AI tools to legacy, large-scale codebases has consistently yielded real productivity gains, but the Instagram Direct team set out with a far more ambitious objective. Instead of simply pointing AI coding assistants at an existing, entrenched codebase, the team fundamentally redesigned the application architecture to be AI-native by design. This foundational shift multiplied the impact of artificial intelligence far beyond what mere retrofitting could ever deliver.

The team selected Jetpack Compose as a cornerstone for this new AI-native UI architecture due to its declarative nature. Declarative code is inherently more concise, predictable, and structurally straightforward for AI models to reason about. It introduces fewer side effects, relies less on implicit state management, and establishes crystal-clear component boundaries.

How Instagram Direct engineers built AI-native UI architecture with Jetpack Compose and reduced token cost per agent session by 33% | Android Developers' Blog

Executing a migration of this magnitude required meticulous planning. Given that hundreds of millions of people rely on Instagram daily to communicate, the transition had to be gradual, seamless, and entirely free of user-facing disruptions while the core foundation was replaced underneath active users. To fully grasp the scale of the engineering challenge, consider that individual UI components within Instagram Direct can render in over 160 distinct state permutations, and a single conversation screen alone manages more than 200 distinct message types.

When migrating codebases of this scale, developers are often tempted to take an incremental shortcut by embedding Compose UI components directly inside existing Android View hierarchies. While such a hybrid approach can be a valid temporary stepping stone during early migration phases, it introduces long-term complications. AI coding tools naturally gravitate toward the path of least resistance. When imperative and declarative UI paradigms are mixed together within the same codebase, AI assistants frequently blend them incorrectly, inadvertently introducing subtle bugs, persistent technical debt, and performance regressions.

Building an AI-Native UI Architecture

At the immense scale of Instagram, a degree of architectural abstraction is entirely unavoidable to keep the application maintainable as it expands. A common architectural pattern involves modeling every recycler view item type as a descendant of a custom base class that exposes standard lifecycle hooks such as binding methods. However, naive implementations often couple imperative context flags directly inside declarative lambdas, causing state variables to survive re-bindings and recycle improperly across rows. This separation failure leads to difficult-to-reproducing bugs and state leakage.

To establish a genuinely AI-friendly codebase, the Instagram engineering team adhered to strict structural rules. List items could still be represented through dedicated abstractions, but all Compose UI code had to be completely isolated within constructors. This restriction prevents UI components from accessing class members or mutable states directly, ensuring their only source of input comes explicitly from constructor parameters. Consequently, these components function identically to plain composable functions while still conforming to the application’s broader architectural patterns.

How Instagram Direct engineers built AI-native UI architecture with Jetpack Compose and reduced token cost per agent session by 33% | Android Developers' Blog

Managing this massive migration required running multiple AI agents against a shared knowledge base of reusable skills and conventions developed specifically for the transition. This shared repository ensured that workflows and best practices remained synchronized across the entire engineering organization, preventing individual developers from having to independently rediscover solutions to complex edge cases. By splitting the migration of each screen into distinct, highly coordinated stages, individual engineers could rapidly settle architectural foundations and tricky edge cases up front. Once that groundwork was established, other team members could focus entirely on polishing the user interface to a production-ready standard without stopping to resolve fundamental structural decisions.

The quantitative results of the migration strongly validated the chosen approach. For every migrated Instagram Direct surface, Jetpack Compose enabled the team to cut the total amount of UI code in half. Empirical data gathered through internal Meta analyses comparing AI agent sessions working on Compose UI against identical tasks executed using legacy Android Views demonstrated profound efficiency gains. Both output efficiency and typical session metrics confirmed that Jetpack Compose significantly reduced resource consumption per unit of landed output as well as total token costs for typical agent sessions.

Moreover, the data revealed a compelling resilience against code complexity. Meta continuously monitors code quality and production risk using a composite risk score that evaluates the likelihood of changes causing incidents. As files accumulate higher risk scores, AI agent sessions naturally become less resource efficient. When a file’s accumulated risk score doubles, user interfaces implemented using legacy Android Views suffer a 30 percent reduction in AI agent resource efficiency per landed character. In contrast, when the same files are implemented using Jetpack Compose UI, the reduction in resource efficiency is restricted to just 9 percent.

Performance Optimizations

Because Instagram Direct is one of the most heavily trafficked and essential surfaces of the application, users expect instantaneous responsiveness at all times. Adopting Jetpack Compose required a substantial rewrite of the user interface, and the absolute priority was preserving an uncompromising, high-quality user experience without introducing performance regressions. Years of prior iteration had driven the legacy View-based implementation to an exceptionally high performance threshold, meaning the new framework had to match or exceed those rigorous benchmarks.

How Instagram Direct engineers built AI-native UI architecture with Jetpack Compose and reduced token cost per agent session by 33% | Android Developers' Blog

The engineering team tracked thousands of performance metrics in production, relying heavily on real-time A/B testing to compare the newly migrated Compose surfaces directly against legacy implementations. Experience showed that small, piecemeal migrations of only a handful of UI components often painted an incomplete or misleading picture, frequently resulting in false negatives regarding framework capabilities. The true performance benefits of Jetpack Compose only became fully apparent when entire surfaces were migrated end-to-end without bridging interruptions.

The core screens within Instagram Direct are structured around long, dynamic lists of varied item types, which were originally powered by standard recycler views. While the architecture relied on custom abstractions to maintain scalability, it remained tethered to the lifecycle constraints of the legacy View system. To overcome this limitation, the team invested in replacing the underlying recycler view architecture with Compose-native alternatives like lazy columns.

To facilitate smooth rollouts, the team engineered abstract Compose components capable of interacting natively with lazy columns while retaining compatibility with legacy recycler views through dedicated interop APIs. This design allowed features to be tested safely via runtime feature flags in production environments without requiring simultaneous wholesale rewrites across unrelated product teams.

Addressing the unique performance sensitivities of Instagram required an intensive, hands-on partnership between Google and Meta engineers. Together, the teams analyzed runtime metrics to pinpoint bottlenecks and design brand-new capabilities for Jetpack Compose that could successfully match or surpass legacy benchmarks. Two major feature additions emerged directly from this collaborative effort: pausable composition enabled by advanced layout cache windows, and sophisticated visibility tracking APIs.

How Instagram Direct engineers built AI-native UI architecture with Jetpack Compose and reduced token cost per agent session by 33% | Android Developers' Blog

Pausable Composition and Advanced Layout Cache Windows

Pausable composition allows expensive lazy-list items to be composed incrementally across multiple animation frames, effectively eliminating frame drops and visual stutters during rapid scrolling. When paired with advanced layout cache window capabilities, this combination significantly enhances scroll smoothness across complex messaging interfaces. Internal testing conducted at Meta demonstrated that combining pausable composition with a one-viewport cache window reduced large frame drops per minute by approximately 13 percent compared to standard Compose implementations, while the cache window feature on its own achieved an 8 percent reduction against the same baseline.

By configuring cache windows to proactively prepare and retain off-screen items within precise pixel bands or fractional viewports around the active screen, applications can achieve lightning-fast flings and instant rendering responsiveness. These technical refinements ensure that even under heavy computational loads, the application maintains buttery-smooth interaction standards.

Impression Logging and Startup Performance

Another critical outcome of the technical partnership between Google and Meta was the introduction of standardized visibility change APIs. These new interfaces provide large-scale Jetpack Compose surfaces with a reliable, built-in mechanism to determine precisely when a composable element is actively visible on the user’s screen, successfully replacing custom, hand-rolled tracking implementations used previously. Within Instagram Direct, these precise visibility signals feed into hundreds of product quality and impression logging metrics that depend on accurate tracking of user views.

Beyond runtime scrolling performance, the adoption of Jetpack Compose delivered unexpected positive side effects regarding application startup speeds. While the Jetpack Compose runtime carries a one-time warmup cost during initial execution, messaging surfaces are frequently accessed immediately upon launching the application. Because users heavily engage with Direct early in their sessions, other independent surfaces throughout the wider Instagram app that relied on Compose also registered noticeable performance enhancements. Startup execution inside Instagram Direct itself was further optimized through the deployment of baseline profiles, which pre-compile critical code paths during application installation to guarantee instantaneous rendering from the very first launch.

How Instagram Direct engineers built AI-native UI architecture with Jetpack Compose and reduced token cost per agent session by 33% | Android Developers' Blog

The successful migration of Instagram Direct stands as a landmark achievement in modern mobile software engineering. By treating framework migration as an opportunity to build an AI-native architecture rather than a simple cosmetic rewrite, Meta and Google have established a powerful blueprint for scaling complex applications while simultaneously empowering developer productivity and application performance.

By Sagoh