https://www.boundaryml.com logo
🟡 Seeking code example for multi-key fallback/rou...
# questions
d
I'm looking for a code example to manage a multi-key fallback/roundrobin within BAML. In some cases, we may have 2 OpenAI keys. I need to use the second key for fall-back in case the first key is hitting rate limits. I create separate clients for them, and use the usual fallback & round-robin providers. Please let me know if this will work, and if there is a better approach.
Copy code
client<llm> CustomGPT4oMini {
  provider openai
  retry_policy Exponential
  options {
    model "gpt-4o-mini"
    api_key env.OPENAI_API_KEY
  }
}

client<llm> CustomGPT41Mini {
  provider openai
  retry_policy Exponential
  options {
    model "gpt-4.1-mini"
    api_key env.OPENAI_API_KEY
  }
}

client<llm> CustomGPT4oMini_Key2 {
  provider openai
  retry_policy Exponential
  options {
    model "gpt-4o-mini"
    api_key env.OPENAI_API_KEY2
  }
}

client<llm> CustomGPT41Mini_Key2 {
  provider openai
  retry_policy Exponential
  options {
    model "gpt-4.1-mini"
    api_key env.OPENAI_API_KEY2
  }
}

client<llm> SingleKeyFallback {
  provider fallback
  retry_policy Exponential
  options {
    strategy [CustomGPT4oMini, CustomGPT41Mini]
  }
}

client<llm> MultiKeyFallback {
  provider fallback
  retry_policy Exponential
  options {
    strategy [CustomGPT4oMini, CustomGPT4oMini_Key2, CustomGPT41Mini, CustomGPT41Mini_Key2]
  }
}
v
This will work! 🙂 If you want to have a rotation between the keys, then you should use a roundrobin If you click on on the client graph button in vscode extension, you’ll be able to viz exactly what this client definition is calling
d
Oh right, had not used the Client graph functional so far
Is the fallback strategy applied for all non-200 status codes?
v
Yep!