Dynamically update your Firebase AI Logic app with Firebase Remote Config  |  Firebase AI Logic

Dynamically update your Firebase AI Logic app with Firebase Remote Config

Firebase Remote Config lets you dynamically adjust your app's behavior without requiring app updates. This is especially powerful for apps that use generative AI, where rapid iteration and fine-tuning are crucial.

Why use Firebase Remote Config with your app?

Essential use cases for Remote Config with generative AI apps

We recommend using Remote Config with Firebase AI Logic for the following essential use cases:

Advanced and recommended use cases for Remote Config with generative AI apps

After instrumenting your app with Remote Config and Google Analytics, you can explore advanced use cases:

By instrumenting your generative AI app with Firebase Remote Config, you can build flexible, safe, and cost-effective AI-powered applications while creating delightful experiences for your users.

Add Firebase Remote Config to your app

In this solution guide, you'll use Firebase Remote Config to dynamically update parameters in your app that use the Firebase AI Logic SDK. You will learn how to:

Prerequisites

This guide assumes that you're familiar with developing apps for your platform.

Before you begin, make sure that you do the following:

Step 1: Set parameter values in the Firebase console

Create a Remote Config client template and configure parameters and values to fetch and use in the app.

  1. In the Firebase console, go to DevOps & Engagement > Remote Config.

  2. From the Client/Server selector at the top of the page, select Client.

  3. Start a client template by clicking Create Configuration (or Add parameter if you've previously used client templates).

  4. Define the parameters you want to control with Remote Config. For example:

    Parameter name Description Type Default value
    model_name Model name. See available model names. String gemini-3.8-flash
    system_instructions System instructions are like a "preamble" that you add before the model gets exposed to any further instructions from the end user to influence model behavior. String You are a helpful assistant who knows everything there is to know about Firebase!
    prompt Default prompt to use with your generative AI feature. String I am a developer who wants to know more about Firebase!
    agent_platform_location Only applicable if using Agent Platform Gemini API (formerly Vertex AI). Control the location to access the model. You can set conditions to configure this option based on client location detected by Google Analytics. String global
  5. When you've finished adding parameters, click Publish changes. If this is not a new Remote Config template, review the changes and click Publish changes again.

Step 2: Add and initialize Remote Config in your app

  1. In Xcode, with the project open, navigate to File > Add Package Dependencies.
  2. Select firebase-ios-sdk and then click Add package.
  3. Add the Remote Config dependency to your module (app-level) Gradle file (usually app/build.gradle.kts or app/build.gradle).
dependencies {
       implementation(platform("com.google.firebase:firebase-bom:34.19.0"))
       implementation("com.google.firebase:firebase-ai")
       implementation("com.google.firebase:firebase-config")
       // ... other dependencies
}
  1. Inside the appropriate class for your app, initialize Firebase and add Remote Config to your main application logic:
let remoteConfig = RemoteConfig.remoteConfig()
let settings = RemoteConfigSettings()
settings.minimumFetchInterval = 3600
remoteConfig.configSettings = settings

Step 3: Set in-app parameter values

You should set in-app default parameter values in the Remote Config object. This ensures that your app behaves as expected even if it cannot fetch values from the Remote Config service.

  1. In the Firebase console, go to the DevOps & Engagement > Remote Config > Parameters page.
  2. Open the more_vert Menu, and select Download default values.
  3. When prompted, enable .plist for iOS, then click Download file.
  4. Save the file in your application directory.
  5. In Xcode, right-click on your app and select Add Files.
  6. Select remote_config_defaults.plist, then click Add.
  7. Update your app code to reference the defaults file:
// Set default values for Remote Config parameters.
remoteConfig.setDefaults(fromPlist: "remote_config_defaults")

Step 4: Fetch and activate values

After setting defaults, add the following to fetch and activate values.

// Fetch and activate Remote Config values
remoteConfig.fetchAndActivate { status, error in
  if let error = error {
    print("Error fetching Remote Config: \(error.localizedDescription)")
  }
}

Step 5: Add a real-time Remote Config listener

Add a real-time Remote Config listener to your app to ensure that changes you make to the Remote Config template are propagated to the client as soon as they're updated.

// Add real-time Remote Config
remoteConfig.addOnConfigUpdateListener { configUpdate, error in
  guard let configUpdate = configUpdate, error == nil else {
    print("Error listening for config updates: \(error?.localizedDescription ?? "No error available")")
    return
  }
  print("Updated keys: \(configUpdate.updatedKeys)")
  remoteConfig.activate { changed, error in
    guard error == nil else {
      print("Error activating config: \(error?.localizedDescription ?? "No error available")")
      return
    }
    print("Activated config successfully")
  }
}

Step 6: Update the Gemini API requests to use Remote Config values

Now that Remote Config is fully configured, update your code to replace hard-coded values with values sourced from Remote Config.

// Initialize the Gemini Developer API backend service
let ai = FirebaseAI.firebaseAI(backend: .googleAI())
let modelName = remoteConfig.configValue(forKey: "model_name").stringValue
let systemInstructions = remoteConfig.configValue(forKey: "system_instructions").stringValue
let userPrompt = remoteConfig.configValue(forKey: "prompt").stringValue
let response = try await model.generateContent(userPrompt)
if let text = response.text {
  print(text)
}

Step 7: Run the app

Build and run the app and verify that it works. Make changes to your configuration from the Remote Config page in the Firebase console, publish the changes, and verify the result.