Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Error’d: You Talkin’ to Me?

      September 20, 2025

      The Psychology Of Trust In AI: A Guide To Measuring And Designing For User Confidence

      September 20, 2025

      This week in AI updates: OpenAI Codex updates, Claude integration in Xcode 26, and more (September 19, 2025)

      September 20, 2025

      Report: The major factors driving employee disengagement in 2025

      September 20, 2025

      DistroWatch Weekly, Issue 1140

      September 21, 2025

      Distribution Release: DietPi 9.17

      September 21, 2025

      Development Release: Zorin OS 18 Beta

      September 19, 2025

      Distribution Release: IPFire 2.29 Core 197

      September 19, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      @ts-ignore is almost always the worst option

      September 22, 2025
      Recent

      @ts-ignore is almost always the worst option

      September 22, 2025

      MutativeJS v1.3.0 is out with massive performance gains

      September 22, 2025

      Student Performance Prediction System using Python Machine Learning (ML)

      September 21, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      DistroWatch Weekly, Issue 1140

      September 21, 2025
      Recent

      DistroWatch Weekly, Issue 1140

      September 21, 2025

      Distribution Release: DietPi 9.17

      September 21, 2025

      Hyprland Made Easy: Preconfigured Beautiful Distros

      September 20, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Machine Learning»A Coding Implementation to Build a Self-Adaptive Goal-Oriented AI Agent Using Google Gemini and the SAGE Framework

    A Coding Implementation to Build a Self-Adaptive Goal-Oriented AI Agent Using Google Gemini and the SAGE Framework

    August 6, 2025

    In this tutorial, we dive into building an advanced AI agent system based on the SAGE framework, Self-Adaptive Goal-oriented Execution, using Google’s Gemini API. We walk through each core component of the framework: Self-Assessment, Adaptive Planning, Goal-oriented Execution, and Experience Integration. By combining these, we aim to create an intelligent, self-improving agent that can deconstruct a high-level goal, plan its steps, execute tasks methodically, and learn from its outcomes. This hands-on walkthrough helps us understand the underlying architecture and also demonstrates how to orchestrate complex decision-making using real-time AI generation. Check out the FULL CODES here.

    Copy CodeCopiedUse a different Browser
    import google.generativeai as genai
    import json
    import time
    from typing import Dict, List, Any, Optional
    from dataclasses import dataclass, asdict
    from enum import Enum
    
    
    class TaskStatus(Enum):
       PENDING = "pending"
       IN_PROGRESS = "in_progress"
       COMPLETED = "completed"
       FAILED = "failed"

    We start by importing the necessary libraries, including google.generativeai for interacting with the Gemini model, and Python modules like json, time, and dataclasses for task management. We define a TaskStatus enum to help us track the progress of each task as pending, in progress, completed, or failed. Check out the FULL CODES here.

    Copy CodeCopiedUse a different Browser
    @dataclass
    class Task:
       id: str
       description: str
       priority: int
       status: TaskStatus = TaskStatus.PENDING
       dependencies: List[str] = None
       result: Optional[str] = None
      
       def __post_init__(self):
           if self.dependencies is None:
               self.dependencies = []
    
    
    class SAGEAgent:
       """Self-Adaptive Goal-oriented Execution AI Agent"""
      
       def __init__(self, api_key: str, model_name: str = "gemini-1.5-flash"):
           genai.configure(api_key=api_key)
           self.model = genai.GenerativeModel(model_name)
           self.memory = []
           self.tasks = {}
           self.context = {}
           self.iteration_count = 0
          
       def self_assess(self, goal: str, context: Dict[str, Any]) -> Dict[str, Any]:
           """S: Self-Assessment - Evaluate current state and capabilities"""
           assessment_prompt = f"""
           You are an AI agent conducting self-assessment. Respond ONLY with valid JSON, no additional text.
    
    
           GOAL: {goal}
           CONTEXT: {json.dumps(context, indent=2)}
           TASKS_PROCESSED: {len(self.tasks)}
          
           Provide assessment as JSON with these exact keys:
           {{
               "progress_score": <number 0-100>,
               "resources": ["list of available resources"],
               "gaps": ["list of knowledge gaps"],
               "risks": ["list of potential risks"],
               "recommendations": ["list of next steps"]
           }}
           """
          
           response = self.model.generate_content(assessment_prompt)
           try:
               text = response.text.strip()
               if text.startswith('```'):
                   text = text.split('```')[1]
                   if text.startswith('json'):
                       text = text[4:]
               text = text.strip()
               return json.loads(text)
           except Exception as e:
               print(f"Assessment parsing error: {e}")
               return {
                   "progress_score": 25,
                   "resources": ["AI capabilities", "Internet knowledge"],
                   "gaps": ["Specific domain expertise", "Real-time data"],
                   "risks": ["Information accuracy", "Scope complexity"],
                   "recommendations": ["Break down into smaller tasks", "Focus on research first"]
               }
      
       def adaptive_plan(self, goal: str, assessment: Dict[str, Any]) -> List[Task]:
           """A: Adaptive Planning - Create dynamic, context-aware task decomposition"""
           planning_prompt = f"""
           You are an AI task planner. Respond ONLY with valid JSON array, no additional text.
    
    
           MAIN_GOAL: {goal}
           ASSESSMENT: {json.dumps(assessment, indent=2)}
          
           Create 3-4 actionable tasks as JSON array:
           [
               {{
                   "id": "task_1",
                   "description": "Clear, specific task description",
                   "priority": 5,
                   "dependencies": []
               }},
               {{
                   "id": "task_2",
                   "description": "Another specific task",
                   "priority": 4,
                   "dependencies": ["task_1"]
               }}
           ]
          
           Each task must have: id (string), description (string), priority (1-5), dependencies (array of strings)
           """
          
           response = self.model.generate_content(planning_prompt)
           try:
               text = response.text.strip()
               if text.startswith('```'):
                   text = text.split('```')[1]
                   if text.startswith('json'):
                       text = text[4:]
               text = text.strip()
              
               task_data = json.loads(text)
               tasks = []
               for i, task_info in enumerate(task_data):
                   task = Task(
                       id=task_info.get('id', f'task_{i+1}'),
                       description=task_info.get('description', 'Undefined task'),
                       priority=task_info.get('priority', 3),
                       dependencies=task_info.get('dependencies', [])
                   )
                   tasks.append(task)
               return tasks
           except Exception as e:
               print(f"Planning parsing error: {e}")
               return [
                   Task(id="research_1", description="Research sustainable urban gardening basics", priority=5),
                   Task(id="research_2", description="Identify space-efficient growing methods", priority=4),
                   Task(id="compile_1", description="Organize findings into structured guide", priority=3, dependencies=["research_1", "research_2"])
               ]
      
       def execute_goal_oriented(self, task: Task) -> str:
           """G: Goal-oriented Execution - Execute specific task with focused attention"""
           execution_prompt = f"""
           GOAL-ORIENTED EXECUTION:
           Task: {task.description}
           Priority: {task.priority}
           Context: {json.dumps(self.context, indent=2)}
          
           Execute this task step-by-step:
           1. Break down the task into concrete actions
           2. Execute each action methodically
           3. Validate results at each step
           4. Provide comprehensive output
          
           Focus on practical, actionable results. Be specific and thorough.
           """
          
           response = self.model.generate_content(execution_prompt)
           return response.text.strip()
      
       def integrate_experience(self, task: Task, result: str, success: bool) -> Dict[str, Any]:
           """E: Experience Integration - Learn from outcomes and update knowledge"""
           integration_prompt = f"""
           You are learning from task execution. Respond ONLY with valid JSON, no additional text.
    
    
           TASK: {task.description}
           RESULT: {result[:200]}...
           SUCCESS: {success}
          
           Provide learning insights as JSON:
           {{
               "learnings": ["key insight 1", "key insight 2"],
               "patterns": ["pattern observed 1", "pattern observed 2"],
               "adjustments": ["adjustment for future 1", "adjustment for future 2"],
               "confidence_boost": <number -10 to 10>
           }}
           """
          
           response = self.model.generate_content(integration_prompt)
           try:
               text = response.text.strip()
               if text.startswith('```'):
                   text = text.split('```')[1]
                   if text.startswith('json'):
                       text = text[4:]
               text = text.strip()
              
               experience = json.loads(text)
               experience['task_id'] = task.id
               experience['timestamp'] = time.time()
               self.memory.append(experience)
               return experience
           except Exception as e:
               print(f"Experience parsing error: {e}")
               experience = {
                   "learnings": [f"Completed task: {task.description}"],
                   "patterns": ["Task execution follows planned approach"],
                   "adjustments": ["Continue systematic approach"],
                   "confidence_boost": 5 if success else -2,
                   "task_id": task.id,
                   "timestamp": time.time()
               }
               self.memory.append(experience)
               return experience
      
       def execute_sage_cycle(self, goal: str, max_iterations: int = 3) -> Dict[str, Any]:
           """Execute complete SAGE cycle for goal achievement"""
           print(f"<img src="https://s.w.org/images/core/emoji/16.0.1/72x72/1f3af.png" alt="🎯" class="wp-smiley" /> Starting SAGE cycle for goal: {goal}")
           results = {"goal": goal, "iterations": [], "final_status": "unknown"}
          
           for iteration in range(max_iterations):
               self.iteration_count += 1
               print(f"n<img src="https://s.w.org/images/core/emoji/16.0.1/72x72/1f504.png" alt="🔄" class="wp-smiley" /> SAGE Iteration {iteration + 1}")
              
               print("<img src="https://s.w.org/images/core/emoji/16.0.1/72x72/1f4ca.png" alt="📊" class="wp-smiley" /> Self-Assessment...")
               assessment = self.self_assess(goal, self.context)
               print(f"Progress Score: {assessment.get('progress_score', 0)}/100")
              
               print("<img src="https://s.w.org/images/core/emoji/16.0.1/72x72/1f5fa.png" alt="🗺" class="wp-smiley" />  Adaptive Planning...")
               tasks = self.adaptive_plan(goal, assessment)
               print(f"Generated {len(tasks)} tasks")
              
               print("<img src="https://s.w.org/images/core/emoji/16.0.1/72x72/26a1.png" alt="⚡" class="wp-smiley" /> Goal-oriented Execution...")
               iteration_results = []
              
               for task in sorted(tasks, key=lambda x: x.priority, reverse=True):
                   if self._dependencies_met(task):
                       print(f"  Executing: {task.description}")
                       task.status = TaskStatus.IN_PROGRESS
                      
                       try:
                           result = self.execute_goal_oriented(task)
                           task.result = result
                           task.status = TaskStatus.COMPLETED
                           success = True
                           print(f"  <img src="https://s.w.org/images/core/emoji/16.0.1/72x72/2705.png" alt="✅" class="wp-smiley" /> Completed: {task.id}")
                       except Exception as e:
                           task.status = TaskStatus.FAILED
                           task.result = f"Error: {str(e)}"
                           success = False
                           print(f"  <img src="https://s.w.org/images/core/emoji/16.0.1/72x72/274c.png" alt="❌" class="wp-smiley" /> Failed: {task.id}")
                      
                       experience = self.integrate_experience(task, task.result, success)
                      
                       self.tasks[task.id] = task
                       iteration_results.append({
                           "task": asdict(task),
                           "experience": experience
                       })
              
               self._update_context(iteration_results)
              
               results["iterations"].append({
                   "iteration": iteration + 1,
                   "assessment": assessment,
                   "tasks_generated": len(tasks),
                   "tasks_completed": len([r for r in iteration_results if r["task"]["status"] == "completed"]),
                   "results": iteration_results
               })
              
               if assessment.get('progress_score', 0) >= 90:
                   results["final_status"] = "achieved"
                   print("<img src="https://s.w.org/images/core/emoji/16.0.1/72x72/1f389.png" alt="🎉" class="wp-smiley" /> Goal achieved!")
                   break
          
           if results["final_status"] == "unknown":
               results["final_status"] = "in_progress"
          
           return results
      
       def _dependencies_met(self, task: Task) -> bool:
           """Check if task dependencies are satisfied"""
           for dep_id in task.dependencies:
               if dep_id not in self.tasks or self.tasks[dep_id].status != TaskStatus.COMPLETED:
                   return False
           return True
      
       def _update_context(self, results: List[Dict[str, Any]]):
           """Update agent context based on execution results"""
           completed_tasks = [r for r in results if r["task"]["status"] == "completed"]
           self.context.update({
               "completed_tasks": len(completed_tasks),
               "total_tasks": len(self.tasks),
               "success_rate": len(completed_tasks) / len(results) if results else 0,
               "last_update": time.time()
           })

    We define a Task data class to encapsulate each unit of work, including its ID, description, priority, and dependencies. Then, we build the SAGEAgent class, which serves as the brain of our framework. It orchestrates the full cycle, self-assessing progress, planning adaptive tasks, executing each task with focus, and learning from outcomes to improve performance in future iterations. Check out the FULL CODES here.

    Copy CodeCopiedUse a different Browser
    if __name__ == "__main__":
       API_KEY = "Use Your Own API Key Here" 
      
       try:
           agent = SAGEAgent(API_KEY, model_name="gemini-1.5-flash")
          
           goal = "Research and create a comprehensive guide on sustainable urban gardening practices"
          
           results = agent.execute_sage_cycle(goal, max_iterations=2)
          
           print("n" + "="*50)
           print("<img src="https://s.w.org/images/core/emoji/16.0.1/72x72/1f4cb.png" alt="📋" class="wp-smiley" /> SAGE EXECUTION SUMMARY")
           print("="*50)
           print(f"Goal: {results['goal']}")
           print(f"Status: {results['final_status']}")
           print(f"Iterations: {len(results['iterations'])}")
          
           for i, iteration in enumerate(results['iterations'], 1):
               print(f"nIteration {i}:")
               print(f"  Assessment Score: {iteration['assessment'].get('progress_score', 0)}/100")
               print(f"  Tasks Generated: {iteration['tasks_generated']}")
               print(f"  Tasks Completed: {iteration['tasks_completed']}")
          
           print("n<img src="https://s.w.org/images/core/emoji/16.0.1/72x72/1f9e0.png" alt="🧠" class="wp-smiley" /> Agent Memory Entries:", len(agent.memory))
           print("<img src="https://s.w.org/images/core/emoji/16.0.1/72x72/1f3af.png" alt="🎯" class="wp-smiley" /> Total Tasks Processed:", len(agent.tasks))
          
       except Exception as e:
           print(f"Demo requires valid Gemini API key. Error: {e}")
           print("Get your free API key from: https://makersuite.google.com/app/apikey")
    

    We wrap up the tutorial by initializing the SAGEAgent with our Gemini API key and defining a sample goal on sustainable urban gardening. We then execute the full SAGE cycle and print a detailed summary, including progress scores, task counts, and memory insights, allowing us to evaluate how effectively our agent performed across iterations.

    In conclusion, we successfully implemented and ran a complete SAGE cycle with our Gemini-powered agent. We observe how the system assesses its progress, dynamically generates actionable tasks, executes them with precision, and refines its strategy through learned experience. This modular design empowers us to extend the framework further for more complex, multi-agent environments or domain-specific applications.


    Check out the FULL CODES here. Feel free to check out our GitHub Page for Tutorials, Codes and Notebooks. Also, feel free to follow us on Twitter and don’t forget to join our 100k+ ML SubReddit and Subscribe to our Newsletter.

    The post A Coding Implementation to Build a Self-Adaptive Goal-Oriented AI Agent Using Google Gemini and the SAGE Framework appeared first on MarkTechPost.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleThis AI Paper Introduces C3: A Bilingual Benchmark Dataset and Evaluation Framework for Complex Spoken Dialogue Modeling
    Next Article OpenAI Just Released the Hottest Open-Weight LLMs: gpt-oss-120B (Runs on a High-End Laptop) and gpt-oss-20B (Runs on a Phone)

    Related Posts

    Machine Learning

    How to Evaluate Jailbreak Methods: A Case Study with the StrongREJECT Benchmark

    September 3, 2025
    Machine Learning

    Announcing the new cluster creation experience for Amazon SageMaker HyperPod

    September 3, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    Ubuntu 25.10 Snapshot 2 is Now Available to Download

    Linux

    CVE-2025-7160 – PHPGurukul Zoo Management System SQL Injection Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-4498 – Simple Bus Reservation System Buffer Overflow Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    CSS Intelligence: Speculating On The Future Of A Smarter Language

    Tech & Work

    Highlights

    Learning Resources

    The 10 Best WordPress Hosting Options for Small Businesses in 2025

    August 10, 2025

    Every small business needs a WordPress host that keeps its website running smoothly without adding…

    CVE-2025-30949 – Telegram Guru Team Site Chat Object Injection Vulnerability

    July 16, 2025

    CVE-2025-5832 – Pioneer DMH-WT7600NEX Code Execution Vulnerability

    June 25, 2025

    CVE-2025-48706 – Coros PACE 3 BLE Out-of-Bounds Read Reboot Vulnerability

    June 20, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.