Generative AI

How to Build a Meta-Consulting Meta-Consultancy that selects dynamic forces among fast, deep, tool-based strategies

We begin this lesson by building a meta – an agent that decides how to think before it thinks. Instead of using the same thinking process for every question, we design a system that analyzes the correlation, chooses between quick reasoning, deep thinking, or tool-based integration, and adapts its behavior in real time. By examining each element, we understand how a smart provider can control its understanding effort, measurement speed and accuracy, and then follow a strategy that adapts to the nature of the problem. By doing this, we get a shift from a reactive response to positive thinking. Look The complete code manual.

import re
import time
import random
from typing import Dict, List, Tuple, Literal
from dataclasses import dataclass, field


@dataclass
class QueryAnalysis:
   query: str
   complexity: Literal["simple", "medium", "complex"]
   strategy: Literal["fast", "cot", "tool"]
   confidence: float
   reasoning: str
   execution_time: float = 0.0
   success: bool = True


class MetaReasoningController:
   def __init__(self):
       self.query_history: List[QueryAnalysis] = []
       self.patterns = {
           'math': r'(d+s*[+-*/]s*d+)|calculate|compute|sum|product',
           'search': r'current|latest|news|today|who is|what is.*now',
           'creative': r'write|poem|story|joke|imagine',
           'logical': r'if.*then|because|therefore|prove|explain why',
           'simple_fact': r'^(what|who|when|where) (is|are|was|were)',
       }


   def analyze_query(self, query: str) -> QueryAnalysis:
       query_lower = query.lower()
       has_math = bool(re.search(self.patterns['math'], query_lower))
       needs_search = bool(re.search(self.patterns['search'], query_lower))
       is_creative = bool(re.search(self.patterns['creative'], query_lower))
       is_logical = bool(re.search(self.patterns['logical'], query_lower))
       is_simple = bool(re.search(self.patterns['simple_fact'], query_lower))
       word_count = len(query.split())
       has_multiple_parts="?" in query[:-1] or ';' in query


       if has_math:
           complexity = "medium"
           strategy = "tool"
           reasoning = "Math detected - using calculator tool for accuracy"
           confidence = 0.9
       elif needs_search:
           complexity = "medium"
           strategy = "tool"
           reasoning = "Current/dynamic info - needs search tool"
           confidence = 0.85
       elif is_simple and word_count < 10:
           complexity = "simple"
           strategy = "fast"
           reasoning = "Simple factual query - fast retrieval sufficient"
           confidence = 0.95
       elif is_logical or has_multiple_parts or word_count > 30:
           complexity = "complex"
           strategy = "cot"
           reasoning = "Complex reasoning required - using chain-of-thought"
           confidence = 0.8
       elif is_creative:
           complexity = "medium"
           strategy = "cot"
           reasoning = "Creative task - chain-of-thought for idea generation"
           confidence = 0.75
       else:
           complexity = "medium"
           strategy = "cot"
           reasoning = "Unclear complexity - defaulting to chain-of-thought"
           confidence = 0.6


       return QueryAnalysis(query, complexity, strategy, confidence, reasoning)

We have set up basic parameters that allow our agent to analyze incoming queries. We explain how we categorize recommendations, find patterns, and determine a consultation strategy. As we build this foundation, we build a brain that determines how we think before we react. Look The complete code manual.

class FastHeuristicEngine:
   def __init__(self):
       self.knowledge_base = {
           'capital of france': 'Paris',
           'capital of spain': 'Madrid',
           'speed of light': '299,792,458 meters per second',
           'boiling point of water': '100°C or 212°F at sea level',
       }
   def answer(self, query: str) -> str:
       q = query.lower()
       for k, v in self.knowledge_base.items():
           if k in q:
               return f"Answer: {v}"
       if 'hello' in q or 'hi' in q:
           return "Hello! How can I help you?"
       return "Fast heuristic: No direct match found."


class ChainOfThoughtEngine:
   def answer(self, query: str) -> str:
       s = []
       s.append("Step 1: Understanding the question")
       s.append(f"  → The query asks about: {query[:50]}...")
       s.append("nStep 2: Breaking down the problem")
       if 'why' in query.lower():
           s.append("  → This is a causal question requiring explanation")
           s.append("  → Need to identify causes and effects")
       elif 'how' in query.lower():
           s.append("  → This is a procedural question")
           s.append("  → Need to outline steps or mechanisms")
       else:
           s.append("  → Analyzing key concepts and relationships")
       s.append("nStep 3: Synthesizing answer")
       s.append("  → Combining insights from reasoning steps")
       s.append("nStep 4: Final answer")
       s.append("  → [Detailed response based on reasoning chain]")
       return "n".join(s)


class ToolExecutor:
   def calculate(self, expression: str) -> float:
       m = re.search(r'(d+.?d*)s*([+-*/])s*(d+.?d*)', expression)
       if m:
           a, op, b = m.groups()
           a, b = float(a), float(b)
           ops = {
               '+': lambda x, y: x + y,
               '-': lambda x, y: x - y,
               '*': lambda x, y: x * y,
               '/': lambda x, y: x / y if y != 0 else float('inf'),
           }
           return ops[op](a, b)
       return None


   def search(self, query: str) -> str:
       return f"[Simulated search results for: {query}]"


   def execute(self, query: str, tool_type: str) -> str:
       if tool_type == "calculator":
           r = self.calculate(query)
           if r is not None:
               return f"Calculator result: {r}"
           return "Could not parse mathematical expression"
       elif tool_type == "search":
           return self.search(query)
       return "Tool execution completed"

We develop engines that do the thinking. We design a fast Heuristic module for simple visualization, an inference engine for deep thinking, and tool functions for clustering or searching. As we use these objects, we configure the agent to easily switch between different modes of intelligence. Look The complete code manual.

class MetaReasoningAgent:
   def __init__(self):
       self.controller = MetaReasoningController()
       self.fast_engine = FastHeuristicEngine()
       self.cot_engine = ChainOfThoughtEngine()
       self.tool_executor = ToolExecutor()
       self.stats = {
           'fast': {'count': 0, 'total_time': 0},
           'cot': {'count': 0, 'total_time': 0},
           'tool': {'count': 0, 'total_time': 0},
       }


   def process_query(self, query: str, verbose: bool = True) -> str:
       if verbose:
           print("n" + "="*60)
           print(f"QUERY: {query}")
           print("="*60)
       t0 = time.time()
       analysis = self.controller.analyze_query(query)


       if verbose:
           print(f"n🧠 META-REASONING:")
           print(f"   Complexity: {analysis.complexity}")
           print(f"   Strategy: {analysis.strategy.upper()}")
           print(f"   Confidence: {analysis.confidence:.2%}")
           print(f"   Reasoning: {analysis.reasoning}")
           print(f"n⚡ EXECUTING {analysis.strategy.upper()} STRATEGY...n")


       if analysis.strategy == "fast":
           resp = self.fast_engine.answer(query)
       elif analysis.strategy == "cot":
           resp = self.cot_engine.answer(query)
       elif analysis.strategy == "tool":
           if re.search(self.controller.patterns['math'], query.lower()):
               resp = self.tool_executor.execute(query, "calculator")
           else:
               resp = self.tool_executor.execute(query, "search")


       dt = time.time() - t0
       analysis.execution_time = dt
       self.stats[analysis.strategy]['count'] += 1
       self.stats[analysis.strategy]['total_time'] += dt
       self.controller.query_history.append(analysis)


       if verbose:
           print(resp)
           print(f"n⏱️  Execution time: {dt:.4f}s")
       return resp


   def show_stats(self):
       print("n" + "="*60)
       print("AGENT PERFORMANCE STATISTICS")
       print("="*60)
       for s, d in self.stats.items():
           if d['count'] > 0:
               avg = d['total_time'] / d['count']
               print(f"n{s.upper()} Strategy:")
               print(f"  Queries processed: {d['count']}")
               print(f"  Average time: {avg:.4f}s")
       print("n" + "="*60)

We bring all the parts together in a unified agent. We organize the flow from the meta – thinking about the execution, tracking the performance, and seeing how each program behaves. As we use this system, we see our agent making a decision, consulting, and synchronizing in real time. Look The complete code manual.

def run_tutorial():
   print("""
   META-REASONING AGENT TUTORIAL
   "When Should I Think Hard vs Answer Fast?"


   This agent demonstrates:
   1. Fast vs deep vs tool-based reasoning
   2. Choosing cognitive strategy
   3. Adaptive intelligence
   """)


   agent = MetaReasoningAgent()
   test_queries = [
       "What is the capital of France?",
       "Calculate 156 * 23",
       "Why do birds migrate south for winter?",
       "What is the latest news today?",
       "Hello!",
       "If all humans need oxygen and John is human, what can we conclude?",
   ]


   for q in test_queries:
       agent.process_query(q, verbose=True)
       time.sleep(0.5)


   agent.show_stats()
   print("nTutorial complete!")
   print("• Meta-reasoning chooses how to think")
   print("• Different queries need different strategies")
   print("• Smart agents adapt reasoning dynamicallyn")

We created a demo runner to demonstrate the agent's capabilities. We feed it various questions and watch how it chooses its strategy and generates answers. As we interact with it, we experience the benefits of personal flexibility. Look The complete code manual.

if __name__ == "__main__":
   run_tutorial()

We start the whole lesson with a simple simple block. We run the demo and look at the full pipeline of meta – interactive performance. As we release this, we complete the journey from design to fully functional agent.

In conclusion, we see how it creates a meta – reasoning agent that allows us to submit responses to a structured pattern and to a coherent intelligence. We see how the agent analyzes each query, selects the appropriate negotiation mode, and executes it successfully while tracking its performance. By designing and experimenting with these things, we gain a working understanding of how advanced agents can control their thinking, exert effort, and deliver better results.


Look The complete code manual. Feel free to take a look at ours GitHub page for tutorials, code and notebooks. Also, feel free to follow us Kind of stubborn and don't forget to join ours 100K + ML Subreddit and sign up Our newsletter. Wait! Do you telegraph? Now you can join us by telegraph.


AsifAzzaq is the CEO of MarktechPost Media Inc.. as a visionary entrepreneur and developer, Asifi is committed to harnessing the power of social intelligence for good. His latest effort is the launch of a media intelligence platform, MarktechPpost, which stands out for its deep understanding of machine learning and deep learning stories that are technically sound and easily understood by a wide audience. The platform sticks to more than two million monthly views, which shows its popularity among the audience.

Follow Marktechpost: Add us as a favorite source on Google.

Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button