

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Build an AI Agent
incomplete
2: Python Setup
incomplete
3: OpenRouter API
incomplete
4: Token Metadata
incomplete
5: User Input
incomplete
6: Multiple Messages
incomplete
7: Verbose Output
incomplete
This lesson's interactive features are locked, please to keep using them
LLM APIs aren't typically used in a "one-shot" manner, for example:
They work the same way ChatGPT works: in a conversation. The conversation has a history, and if we keep track of that history, then with each new prompt, the model can see the entire conversation and respond within the larger context of the conversation.
Importantly, each message in the conversation has a "role." In the context of a chat app like ChatGPT, your conversations would look like this:
The two roles we care about right now are user (messages from us) and assistant (messages from the model). Later we'll add system and tool messages too.
So, while our program will still be "one-shot" for now, let's update our code to store a list of messages in the conversation, and pass in the "role" appropriately.
messages = [
{"role": "user", "content": args.user_prompt},
]
response = client.chat.completions.create(
model="openrouter/free",
messages=messages,
)
In the future, we'll add more messages to the list as the agent does its tasks in a loop.
The solution extracts the API call into a separate generate_content(client, messages) function for better code organization. You can keep your code in main() for now, or extract it to a helper function if you prefer. Both approaches work fine.
If everything is still working normally, submit the CLI tests.