Inspiration

Why should users have to choose between powerful AI and the privacy of their sensitive information?

AI-powered document assistants make it easier to understand complex documents, but many require users to upload their information to cloud servers. Employment agreements, financial invoices and business proposals often contain sensitive information that users may not want to share.

While on-device AI offers a more private alternative, smaller models may struggle with complex analytical tasks.

This inspired us to build ContextShield, a privacy-first hybrid AI workspace that combines the privacy of on-device intelligence with the reasoning capabilities of cloud AI, while keeping users in control of their information.

Our guiding principle is simple: Private by default. Powerful when needed.

What it does

ContextShield allows users to analyze, summarize and ask questions about sensitive documents using on-device Gemini Nano and cloud Gemini through Firebase AI Logic.

Instead of automatically uploading documents to the cloud, ContextShield intelligently determines how each task should be handled.

Key features:

  • Intelligent hybrid routing: Determines whether a task requires on-device inference, deterministic computation or deeper cloud reasoning.
  • Local sensitive information detection: Identifies and redacts sensitive information directly on the user's device.
  • Consent-driven cloud analysis: Shows the exact sanitized content before requesting permission to send it to the cloud.
  • Local token restoration: Restores redacted values in AI-generated responses without sharing the original token mappings with the cloud.
  • Deterministic financial verification: Verifies invoice quantities, amounts, taxes and totals without relying on an LLM for arithmetic.
  • Proof of Privacy: Displays inference source, execution time, shared payload size and cryptographic verification of approved content.
  • Offline intelligence: Supports document processing, OCR, redaction, calculations and on-device inference after initial setup.
  • Document support: Processes PDFs, scanned documents, images and pasted text locally.

ContextShield offers three inference modes:

Mode Behavior
Private Processes document questions entirely on-device.
Smart Starts locally and recommends deeper cloud analysis when beneficial.
Advanced Enables cloud reasoning for eligible questions after explicit user approval.

Every document-related cloud request requires approval of the exact proposed payload.

How we built it

We built ContextShield as a progressive web application using React, TypeScript, Vite, Firebase AI Logic, Gemini Nano and cloud Gemini.

The architecture is designed around five core components.

1. Local document processing

We use PDF.js and Tesseract.js to extract text from PDFs, scanned documents and images directly in the browser. These processing libraries are self-hosted rather than loaded from external CDNs.

2. Privacy and redaction engine

Sensitive information is identified and replaced with stable tokens. Original values and their mappings remain on the device.

Users can inspect, modify and selectively disclose information before approving cloud analysis.

3. Hybrid inference orchestration

Firebase AI Logic powers both local and cloud inference.

We use:

  • ONLY_ON_DEVICE for private document inference.
  • ONLY_IN_CLOUD for explicitly approved cloud analysis.
  • PREFER_ON_DEVICE and PREFER_IN_CLOUD for validated, non-sensitive general prompts.

We intentionally avoid automatic cloud fallback for sensitive document requests because it could bypass the user's consent.

4. Cryptographically bound consent

Every proposed cloud payload is associated with a SHA-256 hash.

If the payload changes after review, the existing approval becomes invalid and the user must approve the updated content.

This ensures that the application sends only the payload the user has approved.

5. Transparent intelligence and financial verification

We combine generative AI with deterministic computation for financial tasks.

The application also uses actual inference-source metadata returned by Firebase AI Logic to show whether a response was generated on-device or in the cloud.

Firebase Hosting provides production deployment, while Firebase App Check with reCAPTCHA Enterprise helps protect the cloud AI integration.

Challenges we ran into

Deciding when cloud AI is actually necessary

Initially, complex questions appeared to be natural candidates for cloud inference. However, testing revealed that many could still be answered locally.

We redesigned the routing architecture to prioritize on-device intelligence and recommend deeper cloud analysis only when it could provide additional value.

Reasoning over redacted financial information

During real-model evaluation, we discovered that replacing financial amounts with opaque tokens could cause cloud Gemini to incorrectly flag valid invoice calculations as inconsistent.

We addressed this by introducing a deterministic calculation engine that verifies arithmetic locally and provides structured findings to the cloud model.

This allows the model to focus on commercial risks, missing supporting evidence and contractual concerns instead of attempting arithmetic on redacted values.

Preventing unintended cloud fallback

Automatic hybrid fallback is useful in many applications, but it creates privacy concerns when handling sensitive documents.

We implemented explicit inference boundaries, payload validation and consent verification to prevent automatic transmission of private document content.

Evaluating AI response quality

A successful model response does not necessarily mean the answer is correct.

We developed an evaluation framework that distinguishes correct, partial, incorrect and unsupported responses, as well as responses requiring human review.

We also measure inference latency, execution source and cloud payload size.

Browser compatibility and production deployment

Gemini Nano's browser and hardware requirements, Firebase App Check configuration and cloud API quota limitations introduced additional engineering challenges.

We implemented clear failure states and offline behavior without silently forwarding private document content to the cloud.

Accomplishments that we're proud of

  • Successfully integrating both on-device Gemini Nano and cloud Gemini through Firebase AI Logic.
  • Building a privacy-first hybrid inference architecture with explicit user consent.
  • Implementing SHA-256-based approval verification for cloud payloads.
  • Combining deterministic financial computation with generative AI reasoning.
  • Successfully running real on-device inference without internet connectivity.
  • Deploying ContextShield using Firebase Hosting and Firebase App Check.
  • Building a comprehensive automated testing and model evaluation framework.

Testing and evaluation results

Metric Result
Unit tests 300 passing in the recorded regression baseline
End-to-end tests 61/61 passing
Latest end-to-end execution 43.2 seconds
Real-model evaluation scenarios 16 across multiple evaluation runs
Deterministic invoice checks 7/7 correct for the sample invoice
Manual offline inference Successfully verified

Our automated tests cover consent enforcement, redaction, inference routing, offline functionality, document processing, accessibility and failure handling.

Automated AI tests use mocked services. Real-model evaluations and manual browser tests are recorded separately to ensure transparent reporting.

What we learned

Building ContextShield taught us that effective AI engineering is not simply about using the most powerful model available.

Different tasks require different approaches. Some benefit from local intelligence, others require deterministic computation, and some genuinely need deeper cloud reasoning.

We learned that:

  • Hybrid AI is most valuable when inference decisions are driven by task requirements rather than model availability alone.
  • Privacy must be an architectural principle, not an additional feature.
  • Generative AI should not replace deterministic computation when exact results are required.
  • Automated redaction has limitations, making human review and explicit consent essential.
  • Transparent inference reporting helps users understand and trust AI-powered applications.
  • Real-model evaluation is essential for discovering problems that conventional automated tests may miss.

Most importantly, we learned that trustworthy AI should make its decisions visible, understandable and controllable by the user.

What's next for ContextShield – Privacy-First Hybrid AI

Our vision is to evolve ContextShield into a trusted intelligence layer for sensitive information.

Future improvements include:

  • Multilingual privacy detection: Improve sensitive information detection across languages and unstructured documents.
  • Multi-document intelligence: Enable document comparison and cross-document reasoning.
  • Advanced financial analysis: Extend deterministic verification to additional financial document formats.
  • Enterprise privacy policies: Introduce configurable disclosure rules and organization-specific privacy controls.
  • Smarter inference routing: Improve context selection and local response quality assessment.
  • Broader device compatibility: Expand support as on-device AI capabilities become available across more browsers and devices.

We believe the future of AI is not exclusively local or exclusively cloud-based. It is hybrid, context-aware and controlled by the user.

Try ContextShield

For the best experience, use a compatible desktop version of Google Chrome with Gemini Nano initialized. Fictional sample documents are included for testing. Cloud analysis is subject to applicable Gemini API terms and free-tier availability.

ContextShield is open-source under the Apache 2.0 License. AI coding assistants were used during development.

ContextShield — Private by default. Powerful when needed.

Built With

Share this project:

Updates

Submission history