Register and share your invite link to earn from video plays and referrals.

Search results for pr
pr community
One keyword maps to one global community path.
Create community
People
Not Found
Tweets including pr
# Practical and Useful Patterns with ADK ## 🎨 Battle-Tested Callback Patterns for Production ADK Agents You know how callbacks work — but how do you actually use them in production? Master ADK's **Callback Design Patterns** for logging, caching, security, and more! 💪 ## 📌 Title Callback Patterns (Design Patterns and Best Practices) ## 🔗 URL ## 🧩 Overview ADK callbacks have well-established patterns that recur in production systems: logging, caching, state management, security guardrails, request/response modification, conditional skipping, and artifact handling. The documentation also defines best practices — single responsibility, performance awareness, idempotency, and error handling — to keep callbacks robust. A critical guideline: **for cross-agent security guardrails, prefer Plugins over Callbacks**. ## 🛠 How to Use **Pattern 1: Logging & Monitoring** `logging_before_tool(ctx, tool, args)` logs the `ctx.invocation_id`, ` and `args` via ` then returns `None` to observe without altering the flow. `logging_after_model(ctx, response)` logs the length of ` with the invocation ID, and likewise returns `None`. **Pattern 2: Caching Strategy** `cache_before_tool(ctx, tool, args)` builds a cache key from ` and `hash(str(args))`, then checks `ctx.state.get(cache_key)`. On a cache hit, it returns the cached value to skip tool execution. On a miss, it returns `None` to proceed. `cache_after_tool(ctx, tool, args, tool_ctx, result)` stores the result in `ctx.state[cache_key]` using the same key, then returns `None` to continue without modification. **Pattern 3: State Management** `state_aware_callback(ctx, req)` retrieves the user tier from `ctx.state.get("user:tier", "free")`, and if the tier is `"premium"`, appends additional instructions to `req.config.system_instruction`. It returns `None` to continue the normal flow. ## 🏗 Practical Usage **Multi-layer defense pattern for production:** As a security guardrail (Plugins are preferred for cross-agent use), `security_before_model(ctx, req)` extracts user input from `req.contents[-1].parts[0].text`, runs `detect_pii()` to check for personal information, and if found, calls `audit_log()` and returns an `LlmResponse` with a rejection message to skip the LLM call. It also runs `detect_injection()` for prompt injection detection, blocking with a similar `LlmResponse` if detected. If neither check triggers, it returns `None` to continue. For tool argument sanitization, `sanitize_before_tool(ctx, tool, args)` checks if ` is `"database_query"` and whether `args.get("query", "")` contains `"DROP"`, returning an error dictionary to block dangerous queries. For artifact persistence, `save_artifact_after_agent(ctx)` calls `generate_report(ctx)` and saves the result via `"execution_report.json", report)`, returning `None`. ## 💡 Use Cases - 📊 **Structured logging**: Emit structured logs with invocation IDs at every execution point - 💾 **API cost reduction**: Cache tool results with before/after patterns to avoid redundant calls - 🔐 **Layered security**: Place PII detection, injection prevention, and SQL sanitization at different layers - 📦 **Artifact management**: Auto-save execution results and reports as artifacts - 🎚️ **Dynamic behavior**: Adjust instructions dynamically based on user tier or session state ## ⚠️ Caveats - **Single responsibility**: Give each callback one purpose — don't mix logging with validation - **Performance**: Callbacks execute synchronously; avoid blocking I/O or heavy computation - **Idempotency**: Design callbacks with external side effects to be safe when retried - **Error handling**: Always wrap in try-except to prevent callback errors from crashing the process - **Prefer Plugins**: For cross-agent security policies, consider **Plugins** over per-agent callbacks ## ✨ Closing Knowing callback patterns dramatically levels up your ADK skills. Combine logging, caching, security, and state management patterns to build robust, cost-efficient agents. And for cross-cutting security concerns, don't forget Plugins! #ADK# #AIAgent#
Show more
Prepping For A Cashless Control Grid How Digital Currency Becomes Digital Control
Prince Harry gives update on how Prince Archie and Princess Lilibet are settling into their UK school
Prince Harry gives update on how Prince Archie and Princess Lilibet are settling into their UK school
President Donald Trump has been under fire for not doing more to address concerns about rising prices for consumer goods, particularly as oil prices remain persistently high
ProgramDistill From Interactive Web Apps to Verifiable Reference-Guided SWE Tasks paper:
President Trump received a warm welcome while attending a college football game in Tennessee on Saturday.
President Trump has inadvertently reduced global fossil fuel consumption because of the ongoing war with Iran, per WaPo People worldwide are using significantly less oil and gas, which means less climate pollution.
Show more
0
128
1.6K
80
Forward to community
Presley Gerber’s ex-girlfriend says they’re ‘connected for life’ in heartbreaking new tribute
Presley Gerber’s ex-girlfriend says they’re ‘connected for life’ in heartbreaking new tribute