A basic agent on OCI
Agentic AI Foundations · Lesson 14 of 20 · 1:41:08
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the Oracle Agentic AI Foundations Associate exam (1Z0-1157-26). In this lesson: what to prepare before you code (a project, a model, and authentication with an API key or IAM), the OpenAI client pointed at OCI, a tool loop with function calling, and conversations. Includes three original practice questions, with Fairhaven Furniture, a fictional furniture seller, as the example.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
A first agent on OCI
Fairhaven's developer wants a first agent on OCI, with no server to deploy. It should answer a customer's question through the OCI Responses API, and call a function that checks stock. Then it should keep the conversation going across turns.
Before you code
Before writing any code, you need four things. First, a compartment, with IAM permissions to create Generative AI resources. Second, a project, because a project is required to call the OpenAI-compatible API for agent tasks, and you copy its OCID. Third, a Generative AI API key, with two secrets that work the same way, each with an expiration date, plus an IAM policy that lets the key call the service. And fourth, the OpenAI SDK, not the OCI SDK, because the Responses API is called with OpenAI's client.
Region and model
Then pick a region and a model. Each region offers its own list of models, so check the documentation's table of models by region, and keep the API key in the same region as the model. On demand, you pass the model's ID, and with a dedicated AI cluster, you pass the cluster endpoint's OCID instead. And remember that some project settings, like the memory models, are chosen when you create the project, and cannot be changed later.
The client
The code starts with the standard OpenAI client. Three settings make it run on OCI. The base URL points to OCI Generative AI, in your region. The API key comes from an environment variable, never from the code itself. And the project parameter carries your project's OCID, so every response and conversation lands in that project. Then the create method of responses sends a model ID and an input, and output_text holds the answer.
API keys or IAM
There are two ways to authenticate. A Generative AI API key is a secret token for this service only, which is different from an IAM API signing key, a key pair for general OCI access. Oracle recommends API keys for testing and early development, and IAM-based authentication for production. With the oci-genai-auth package, the OpenAI client signs its requests with OCI identities, like your session on a laptop, or a resource principal when the code runs in an OCI-managed environment.
A tool loop
Now the stock check. The model never sees your Python: you describe the function in JSON, with its name, a description, and its parameters, and you pass it in tools. When the model wants it, the response contains function call items, with arguments, and a call ID. Your code runs the function, then sends back a function call output with the same call ID, chained to the previous response. You repeat until no more calls come back: unlike LangChain or the Agents SDK, the raw API leaves this loop to you.
Conversations and compaction
For a chat that lasts several turns, create a conversation once, with the Conversations API, and pass its ID in each Responses API call. The service keeps the turns, as short-term memory, in your project, for the retention period that the project sets. When a conversation grows long, compaction reduces its history: you can call the compact endpoint yourself, or turn on automatic compaction, with a token threshold. Long-term memory and its models, by contrast, are settings that you choose when you create the project.
Recap
Let's recap. Before you code, you need a project and its OCID, a Generative AI API key in the model's region with an IAM policy, and the OpenAI SDK. The client is the standard OpenAI client, with OCI's base URL, your key, and your project. API keys suit testing, IAM-based authentication suits production, and with function tools, the raw API leaves the loop to your code. And a conversation ID keeps context across turns, while compaction keeps long conversations small.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
A Fairhaven developer created an API key in the Frankfurt region, then called a model through the Chicago endpoint, and every request is rejected. What is the most likely fix?
- Create the API key in the model’s region
- Use the second secret of the same key
- Switch from the OpenAI SDK to the OCI SDK
- Add more tools to the request
Show the answer
Create the API key in the model’s region
An OCI Generative AI API key must be in the same region as the model that it calls. “Use the second secret of the same key” changes nothing, because both secrets of a key give the same access. “Switch from the OpenAI SDK to the OCI SDK” is backwards, since the Responses API is called with the OpenAI SDK. And “Add more tools to the request” has nothing to do with authentication.
Fairhaven’s agent goes to production in an OCI-managed environment, and the security team wants no long-lived secret in its configuration. How should the client authenticate?
- With an API key stored in an environment file
- With both secrets of one API key, rotated
- With IAM-based authentication
- With the project OCID only
Show the answer
With IAM-based authentication
IAM-based authentication, through the oci-genai-auth package, lets the client use the identity of its OCI environment, with no long-lived secret to store, and it is the recommended choice for production. “With an API key stored in an environment file” still stores a secret, even outside the code. “With both secrets of one API key, rotated” also relies on secrets, just two of them. And “With the project OCID only” would identify the project, but it does not authenticate anyone.
Fairhaven’s chat app runs on several servers, and the team does not want to store the history of each customer’s session in the app. Each new message must still have the context of earlier turns. What should they do?
- Resend the full history from a database
- Pass a conversation ID in each request
- Enable long-term memory only
- Create a new project per session
Show the answer
Pass a conversation ID in each request
The Conversations API creates a conversation that the service keeps, and each request that passes its ID gets the earlier turns. “Resend the full history from a database” is exactly the history that the team does not want to store. “Enable long-term memory only” recalls key facts across conversations, but it does not carry every turn of a session. And “Create a new project per session” would isolate each session, without adding any memory.