How to Use OCI Generative AI Agents#
OCI GenAI Agents is a service to create agents in the OCI console. These agents are defined remotely, including their tools, prompts, and optional documents for retrieval-augmented generation (RAG), and can be used for inference.
In this guide, you will learn how to connect an OCI agent using the OciAgent class from the wayflowcore
package.
Basic usage#
To get started, first create your OCI Agent in the OCI Console. Consult the OCI documentation for detailed steps: https://docs.oracle.com/en-us/iaas/Content/generative-ai-agents/home.htm.
Next, create an OciClientConfig
object to configure the connection to the OCI service.
See the OCI LLM configuration for detailed instructions how to configure this object.
You will also need the agent_endpoint_id
from the OCI Console.
This ID points to the agent you want to connect to, while the client configuration is about connecting to the entire service.
Once these are in place, you can create your agent in a few lines:
from wayflowcore.models.ociclientconfig import OCIClientConfigWithApiKey
from wayflowcore.ociagent import OciAgent
oci_config = OCIClientConfigWithApiKey(service_endpoint="OCIGENAI_ENDPOINT")
agent = OciAgent(
agent_endpoint_id="AGENT_ENDPOINT",
client_config=oci_config,
)
Then, use the agent as shown below:
from wayflowcore.executors.executionstatus import UserMessageRequestStatus
# With a linear conversation
conversation = agent.start_conversation()
conversation.append_user_message("What is the answer to 2+2?")
status = conversation.execute()
if isinstance(status, UserMessageRequestStatus):
assistant_reply = conversation.get_last_message()
print(f"---\nAssistant >>> {assistant_reply.content}\n---")
else:
print(f"Invalid execution status, expected UserMessageRequestStatus, received {type(status)}")
# %%
# Or with an execution loop
# inputs = {}
# conversation = assistant.start_conversation(inputs)
# # What is the answer to 2+2?
# while True:
# status = conversation.execute()
# if isinstance(status, FinishedStatus):
# break
# assistant_reply = conversation.get_last_message()
# if assistant_reply is not None:
# print("\nAssistant >>>", assistant_reply.content)
# user_input = input("\nUser >>> ")
# conversation.append_user_message(user_input)
Agent Spec Exporting/Loading#
You can export the assistant configuration to its Agent Spec configuration using the AgentSpecExporter
.
from wayflowcore.agentspec import AgentSpecExporter
serialized_assistant = AgentSpecExporter().to_json(agent)
Here is what the Agent Spec representation will look like ↓
Click here to see the assistant configuration.
{
"component_type": "OciAgent",
"id": "be5b6589",
"name": "oci_agent_be5b6589",
"description": "",
"metadata": {
"__metadata_info__": {
"name": "oci_agent_be5b6589",
"description": ""
}
},
"inputs": [],
"outputs": [],
"agent_endpoint_id": "AGENT_ENDPOINT",
"client_config": {
"component_type": "OciClientConfigWithApiKey",
"id": "ceb3acb1-d2c2-4aed-8f4e-c1cd24daa40c",
"name": "oci_client_config",
"description": null,
"metadata": {},
"service_endpoint": "OCIGENAI_ENDPOINT",
"auth_type": "API_KEY",
"auth_profile": "DEFAULT",
"auth_file_location": "~/.oci/config"
},
"agentspec_version": "25.4.1"
}
component_type: OciAgent
id: be5b6589
name: oci_agent_be5b6589
description: ''
metadata:
__metadata_info__:
name: oci_agent_be5b6589
description: ''
inputs: []
outputs: []
agent_endpoint_id: AGENT_ENDPOINT
client_config:
component_type: OciClientConfigWithApiKey
id: ceb3acb1-d2c2-4aed-8f4e-c1cd24daa40c
name: oci_client_config
description: null
metadata: {}
service_endpoint: OCIGENAI_ENDPOINT
auth_type: API_KEY
auth_profile: DEFAULT
auth_file_location: ~/.oci/config
agentspec_version: 25.4.1
You can then load the configuration back to an assistant using the AgentSpecLoader
.
from wayflowcore.agentspec import AgentSpecLoader
agent: OciAgent = AgentSpecLoader().load_json(serialized_assistant)
Next steps#
Now that you have learned how to use OCI agents in WayFlow, you may proceed to How to Use Agents in Flows.
Full code#
Click on the card at the top of this page to download the full code for this guide or copy the code below.
1# Copyright © 2025 Oracle and/or its affiliates.
2#
3# This software is under the Universal Permissive License
4# %%[markdown]
5# Code Example - How to Use OCI Agents
6# ------------------------------------
7
8# How to use:
9# Create a new Python virtual environment and install the latest WayFlow version.
10# ```bash
11# python -m venv venv-wayflowcore
12# source venv-wayflowcore/bin/activate
13# pip install --upgrade pip
14# pip install "wayflowcore==26.1"
15# ```
16
17# You can now run the script
18# 1. As a Python file:
19# ```bash
20# python howto_ociagent.py
21# ```
22# 2. As a Notebook (in VSCode):
23# When viewing the file,
24# - press the keys Ctrl + Enter to run the selected cell
25# - or Shift + Enter to run the selected cell and move to the cell below# (UPL) 1.0 (LICENSE-UPL or https://oss.oracle.com/licenses/upl) or Apache License
26# 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0), at your option.
27
28
29
30# %%[markdown]
31## Creating the agent
32
33# %%
34from wayflowcore.models.ociclientconfig import OCIClientConfigWithApiKey
35from wayflowcore.ociagent import OciAgent
36
37oci_config = OCIClientConfigWithApiKey(service_endpoint="OCIGENAI_ENDPOINT")
38
39agent = OciAgent(
40 agent_endpoint_id="AGENT_ENDPOINT",
41 client_config=oci_config,
42)
43
44# %%[markdown]
45## Running the agent
46
47# %%
48from wayflowcore.executors.executionstatus import UserMessageRequestStatus
49
50# With a linear conversation
51conversation = agent.start_conversation()
52
53conversation.append_user_message("What is the answer to 2+2?")
54status = conversation.execute()
55if isinstance(status, UserMessageRequestStatus):
56 assistant_reply = conversation.get_last_message()
57 print(f"---\nAssistant >>> {assistant_reply.content}\n---")
58else:
59 print(f"Invalid execution status, expected UserMessageRequestStatus, received {type(status)}")
60
61# %%
62# Or with an execution loop
63# inputs = {}
64# conversation = assistant.start_conversation(inputs)
65
66# # What is the answer to 2+2?
67
68# while True:
69# status = conversation.execute()
70# if isinstance(status, FinishedStatus):
71# break
72# assistant_reply = conversation.get_last_message()
73# if assistant_reply is not None:
74# print("\nAssistant >>>", assistant_reply.content)
75# user_input = input("\nUser >>> ")
76# conversation.append_user_message(user_input)
77
78# %%[markdown]
79## Export config to Agent Spec
80
81# %%
82from wayflowcore.agentspec import AgentSpecExporter
83
84serialized_assistant = AgentSpecExporter().to_json(agent)
85
86# %%[markdown]
87## Load Agent Spec config
88
89# %%
90from wayflowcore.agentspec import AgentSpecLoader
91
92agent: OciAgent = AgentSpecLoader().load_json(serialized_assistant)