- July 12, 2024
- 5 min read
Showcasing FriendliAI’s Integration with LiteLLM

LiteLLM recently introduced FriendliAI as one of their LLM inference API providers. LiteLLM allows users to utilize over 100 large language models with load balancing, fallbacks, and cost tracking, all in the OpenAI API format. You can leverage FriendliAI’s blazing-fast performance and cost-efficiency alongside LiteLLM’s versatile features.
This blog post will explore how the Friendli Serverless Endpoint can be used with LiteLLM. We will cover basic usages, example codes for different response types, and the budget manager provided by LiteLLM. Moreover, stay tuned for a fun experiment comparing the cost-efficiency of FriendliAI and OpenAI models using the budget manager. Based on this experiment, we can generate approximately ten times more tokens with FriendliAI’s meta-llama-3-70b-instruct model than with OpenAI’s GPT-4o model under the same budget conditions. By the end, you'll be well-equipped to maximize your use of LiteLLM and FriendliAI for your specific needs. So please follow along!
Resources
Basic Usages
This section will cover the basic usages of the LiteLLM Python SDK for chat completions with four different response types: default, streaming, asynchronous, and asynchronous streaming. Throughout this blog, we will use FriendliAI’s meta-llama-3-70b-instruct model and ask it “Hello from LiteLLM”.
Before diving in, make sure you have a Friendli Personal Access Token. You can get your token here. You can install the required libraries and export relevant variables as:
Default Example Code
This example demonstrates how you can use the LiteLLM Python SDK to generate a response. LiteLLM supports LLM inferences using the ‘completion’ function.
Streaming Example Code
This example demonstrates how you can use the LiteLLM Python SDK to generate a streaming response. Responses can be streamed by setting the stream argument as ‘True’ in the completion function.
Async Example Code
This example demonstrates how you can use the LiteLLM Python SDK to generate an asynchronous response. Asynchronous chat completions are supported using the ‘acompletion’ function.
Async Streaming Example Code
This example demonstrates how you can use the LiteLLM Python SDK to generate an asynchronous streaming response.
Results
The chat completion inference result of “Hello from LiteLLM” using FriendliAI’s meta-llama-3-70b-instruct model with LiteLLM is as follows:
Congratulations on getting the basics under your belt! You have taken the first step in leveraging LiteLLM and FriendliAI for your projects. As the LLM has answered, how's your day going so far? We hope it has been productive and enjoyable. Furthermore, pay attention to the ‘total_tokens’ variable in the response above. We will use this variable to calculate the total number of tokens used in our final experiment.
Stay tuned as we delve deeper into more advanced features and exciting experiments in the following sections. Let's continue exploring the full potential of these powerful tools together!
Budget Manager
An interesting feature of LiteLLM is their BudgetManager class. You can manage budgets and track spent costs for each user. Advanced features include storing user budgets in a database and resetting user budgets based on a set duration. You can check out their implementation code here.
User-based Rate Limiting Code
In this example, we will explore how to use the BudgetManager class to manage and enforce user-specific budgets. This feature is particularly useful for controlling the costs associated with running LLM inferences. The code goes through the process of creating a budget for a user, checking their current usage against the budget, and updating the cost after an inference is made.
Here's the code implementation:
The Final Budget Manager Experiment
Now that we have finally covered all the basics, let's try something fun! Have you ever wanted to see how many inferences you could make with a strict budget? This experiment can help us understand how much LLMs actually cost. We tried using the budget manager to see how many inferences could be made to the FriendliAI’s meta-llama-3-70b-instruct model with $0.001. Let’s try asking the model “Hello from LiteLLM” until we run out of money.
Here's the code implementation. It tracks and updates the total number of inferences and tokens used, and stops when the budget is exceeded, printing a summary.:
FriendliAI’s meta-llama-3-70b-instruct Model Results
In this run, 27 inferences, using a total of 1281 tokens, could be made with $0.001.
OpenAI’s GPT-4o Model Results
Next, we tried running the same experiment with OpenAI’s GPT-4o model. Simply swap the model value with "gpt-4o" in the experiment code. In this run, 6 inferences, using a total of 126 tokens, could be made with $0.001. Under the same budget, we were able to use over 10 times as many tokens with FriendliAI’s meta-llama-3-70b-instruct model compared to OpenAI’s GPT-4o model!
Token Cost Comparison
This graph visualizes the comparison of the number of tokens generated by LLM models from FriendliAI and OpenAI within a $0.001 budget on LiteLLM.:

With $0.001, we can generate ~10.17 times more tokens with FriendliAI’s meta-llama-3-70b-instruct model (1281 tokens) compared to OpenAI’s GPT-4o model (126 tokens).
Similarly, we can compare the cost per 1M tokens for FriendliAI and OpenAI models as below:
Conclusion
This tutorial shows basic examples of integrating LiteLLM with Friendli Serverless Endpoints for chat completions. We also demonstrate LiteLLM’s budget manager to limit user inference costs. Combining these learnings, we present a practical experiment that calculates the number of inference requests that could be made under a specific budget.
Remember, this is just a starting point – feel free to experiment and customize the process to suit your specific needs using Friendli Endpoints on LiteLLM’s versatile platform!
Written by
FriendliAI Tech & Research
Share
General FAQ
What is FriendliAI?
FriendliAI is the Frontier Inference Cloud for Agents, delivering high throughput, low latency, and reliability at scale for agentic workloads. Through vertically optimized inference infrastructure, it delivers 2–5× faster output token speed and a 99.99% uptime SLA for high-volume production traffic.
How does FriendliAI reduce inference costs?
FriendliAI reduces inference costs through higher GPU utilization and optimized inference performance. FriendliAI's patented continuous batching technique, along with quantization, speculative decoding, KV cache offloading, multi-LoRA serving, and autoscaling, helps you serve more tokens with fewer GPUs, lowering your infrastructure costs without sacrificing performance.
Why should I choose FriendliAI over other inference providers?
FriendliAI is built for production AI agents, combining speed, reliability, and efficiency at scale. It delivers low-latency streaming, reliable long-context inference, and robust tool calling without compromising stability. According to independent OpenRouter benchmarks, FriendliAI consistently ranks among the top providers for throughput, latency, and reliability across leading open-weight models. See why customers choose FriendliAI
Which open-weight models does FriendliAI support?
Run today’s frontier open-weight models—including GLM, MiniMax, Kimi, DeepSeek, Qwen, Gemma, and more—with a simple API call. FriendliAI Model API gives you instant access to the latest models with optimized inference performance for production workloads. Explore models and pricing
How do I get started?
Getting started takes just a few minutes. [1] Sign up for FriendliAI, [2] Generate your API key, and [3] Make your first inference request with frontier open-weight models.
Still have questions?
If you want a customized solution for that key issue that is slowing your growth, support@friendli.ai or click Talk to an engineer — our engineers (not a bot) will reply within one business day.

